ios - 如何弹出回到 Root View Controller 然后推送到不同的 View ?

标签 ios xcode cocoa uiviewcontroller uinavigationcontroller

我正在编写一个具有 3 个 View Controller 的简单应用程序。 Root View Controller 是一个项目列表,基本 TableView 。离开这个 View Controller ,我根据一些用户交互推送两个不同的 View Controller - create item View Controller 或 view item View Controller 。

因此, Storyboard 片段看起来就像 V 之类的东西。

在我的 create item View Controller 上,我希望它在用户创建新项目时弹出回 Root View Controller ,然后推送到 view item Controller ,以便我可以查看新创建的项目。

我似乎无法让它工作。弹回 Root View Controller 很容易,但我无法推送那个 view item Controller 。

有什么想法吗?我已经在下面粘贴了我的代码。弹出功能有效,但新 View 从未出现。

- (void) onSave:(id)sender {

    CLLocation *currentLocation = [[LocationHelper sharedInstance] currentLocation];

    // format the thread object dictionary
    NSArray* location = @[ @(currentLocation.coordinate.latitude), @(currentLocation.coordinate.longitude) ];
    NSDictionary* thread = @{ @"title": _titleField.text, @"text": _textField.text, @"author": @"mustached-bear", @"location": location };

    // send the new thread to the api server
    [[DerpHipsterAPIClient sharedClient] postPath:@"/api/thread"
                                       parameters:thread
                                          success:^(AFHTTPRequestOperation *operation, id responseObject) {

                                              // init thread object
                                              Thread *thread = [[Thread alloc] initWithDictionary:responseObject];

                                              // init view thread controller
                                              ThreadViewController *viewThreadController = [[ThreadViewController alloc] init];
                                              viewThreadController.thread = thread;

                                              [self.navigationController popToRootViewControllerAnimated:NO];
                                              [self.navigationController pushViewController:viewThreadController animated:YES];

                                          }
                                          failure:^(AFHTTPRequestOperation *operation, NSError *error) {

                                              [self.navigationController popToRootViewControllerAnimated:YES];

                                          }];

}

最佳答案

如果我没理解错的话,你有一堆 View Controller :

A (root) - B - C - D - E

你希望它变成:

A (root) - F

对吧?在这种情况下:

NSArray *viewControllers = self.navigationController.viewControllers;
NSMutableArray *newViewControllers = [NSMutableArray array];

// preserve the root view controller
[newViewControllers addObject:[viewControllers objectAtIndex:0]];
// add the new view controller
[newViewControllers addObject:viewThreadController];
// animatedly change the navigation stack
[self.navigationController setViewControllers:newViewControllers animated:YES];

swift 4

// get current view controllers in stack and replace them
let viewControllers = self.navigationController!.viewControllers
let newViewControllers = NSMutableArray()

// preserve the root view controller
newViewControllers.add(viewControllers[0])
// add the new view controller
newViewControllers.add(viewThreadController)
// animatedly change the navigation stack
self.navigationController?.setViewControllers(newViewControllers as! [UIViewController], animated: true)

关于ios - 如何弹出回到 Root View Controller 然后推送到不同的 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11820934/

相关文章:

ios - 使用 RxSwift,如何根据有效文本启用 UIButton?

ios - 如何在 iOS 中获取基本的通话、消息和数据使用统计信息?

objective-c - 两个 UIViewController 的故事

iOS UITextView 的属性文本不从 View 顶部显示

ios - 如何在复杂的层次结构中上下同步 CALayer 和 UIView 动画

ios - 如何通过使用 Ionic1 在 iPhone/iPad 上点击下一个键转到下一个输入?

c++ - 在 Xcode 中使用 fstream 时遇到问题

cocoa - NSNotificationCenter 为 'observer' 和 'object' 保留什么类型的引用?

objective-c - 有没有办法从 Windows 机器编译 mac 二进制文件?

objective-c - 将 NSAlert beginSheetModalForWindow 与 contextInfo 结合使用