我正在编写一个具有3个视图控制器的简单应用程序.根视图控制器是一个项目列表,基本表视图.关闭这个视图控制器,我推送两个不同的视图控制器基于一些用户交互 – 创建项目视图控制器或视图项目视图控制器.
所以,故事板看起来就像一个V,或者什么.
在我的创建项目视图控制器上,当用户创建一个新项目时,我希望它回到根视图控制器,然后推送到视图项目控制器,以便我可以查看新创建的项目.
我似乎无法让这个工作.很容易回到根视图控制器,但是我无法推送该视图项目控制器.
有任何想法吗?我在下面粘贴了我的代码. pop功能有效,但新视图不会出现.
- - (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];
- }];
- }
解决方法
完成您想要做的一个简单的方法是在主根视图控制器中构建一些简单的逻辑 – (void)viewWillAppear方法,并使用委托回调来翻转逻辑开关.基本上是根控制器的“反参考”.这是一个快速的例子.
主控制器(考虑这个控制器a) – 把它称为控制器A
设置属性以跟踪跳转状态
- @property (nonatomic) BOOL jumpNeeded;
设置一些逻辑
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
- self.jumpNeeded ? NSLog(@"jump needed") : NSLog(@"no jump needed");
- if (self.jumpNeeded) {
- NSLog(@"jumping");
- self.jumpNeeded = NO;
- [self performSegueWithIdentifier:@"controllerC" sender:self];
- }
- }
现在,在你的主根控制器中,当选择一个tableview行时,会做这样的事情
当你在tableView中调用controllerB时,选择了方法
- [self performSegueWithIdentifer@"controllerB" sender:self];
- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
- //setup controller B
- if([segue.identifier isEqualTo:@"controllerB"]){
- ControllerB *b = segue.destinationViewController;
- b.delegate = self; //note this is the back reference
- }
- //implement controller c here if needed
- }
现在转到controllerB
您需要设置一个名为“delegate”的属性来保存后面的引用
您需要从根控制器导入头文件
- #import "controllerA"
- @property (nonatomic,weak) controllerA *delegate;
那么在你弹回到controllerA之前,你设置了标志
- self.delegate.jumpNeeded = YES;
- [self.navigationController popViewControllerAnimated:YES];
就是这样.你不必与controllerC做任何事情.还有一些其他的方法可以做,但这是很简单的,为您的需要.希望它适合你.