我正在编写一个具有 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/