ios – 如何弹回到根视图控制器,然后再推到不同的视图?

前端之家收集整理的这篇文章主要介绍了ios – 如何弹回到根视图控制器,然后再推到不同的视图?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在编写一个具有3个视图控制器的简单应用程序.根视图控制器是一个项目列表,基本表视图.关闭这个视图控制器,我推送两个不同的视图控制器基于一些用户交互 – 创建项目视图控制器或视图项目视图控制器.

所以,故事板看起来就像一个V,或者什么.

在我的创建项目视图控制器上,当用户创建一个新项目时,我希望它回到根视图控制器,然后推送到视图项目控制器,以便我可以查看新创建的项目.

我似乎无法让这个工作.很容易回到根视图控制器,但是我无法推送该视图项目控制器.

有任何想法吗?我在下面粘贴了我的代码. pop功能有效,但新视图不会出现.

  1. - (void) onSave:(id)sender {
  2.  
  3. CLLocation *currentLocation = [[LocationHelper sharedInstance] currentLocation];
  4.  
  5. // format the thread object dictionary
  6. NSArray* location = @[ @(currentLocation.coordinate.latitude),@(currentLocation.coordinate.longitude) ];
  7. NSDictionary* thread = @{ @"title": _titleField.text,@"text": _textField.text,@"author": @"mustached-bear",@"location": location };
  8.  
  9. // send the new thread to the api server
  10. [[DerpHipsterAPIClient sharedClient] postPath:@"/api/thread"
  11. parameters:thread
  12. success:^(AFHTTPRequestOperation *operation,id responSEObject) {
  13.  
  14. // init thread object
  15. Thread *thread = [[Thread alloc] initWithDictionary:responSEObject];
  16.  
  17. // init view thread controller
  18. ThreadViewController *viewThreadController = [[ThreadViewController alloc] init];
  19. viewThreadController.thread = thread;
  20.  
  21. [self.navigationController popToRootViewControllerAnimated:NO];
  22. [self.navigationController pushViewController:viewThreadController animated:YES];
  23.  
  24. }
  25. failure:^(AFHTTPRequestOperation *operation,NSError *error) {
  26.  
  27. [self.navigationController popToRootViewControllerAnimated:YES];
  28.  
  29. }];
  30.  
  31. }

解决方法

完成您想要做的一个简单的方法是在主根视图控制器中构建一些简单的逻辑 – (void)viewWillAppear方法,并使用委托回调来翻转逻辑开关.基本上是根控制器的“反参考”.这是一个快速的例子.

主控制器(考虑这个控制器a) – 把它称为控制器A
设置属性以跟踪跳转状态

  1. @property (nonatomic) BOOL jumpNeeded;

设置一些逻辑

  1. - (void)viewWillAppear:(BOOL)animated {
  2. [super viewWillAppear:animated];
  3. self.jumpNeeded ? NSLog(@"jump needed") : NSLog(@"no jump needed");
  4.  
  5. if (self.jumpNeeded) {
  6. NSLog(@"jumping");
  7. self.jumpNeeded = NO;
  8. [self performSegueWithIdentifier:@"controllerC" sender:self];
  9. }
  10. }

现在,在你的主根控制器中,当选择一个tableview行时,会做这样的事情
当你在tableView中调用controllerB时,选择了方法

  1. [self performSegueWithIdentifer@"controllerB" sender:self];

后执行你的准备segue方法

  1. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  2.  
  3. //setup controller B
  4. if([segue.identifier isEqualTo:@"controllerB"]){
  5. ControllerB *b = segue.destinationViewController;
  6. b.delegate = self; //note this is the back reference
  7. }
  8.  
  9. //implement controller c here if needed
  10. }

现在转到controllerB
您需要设置一个名为“delegate”的属性来保存后面的引用
您需要从根控制器导入头文件

  1. #import "controllerA"
  2.  
  3. @property (nonatomic,weak) controllerA *delegate;

那么在你弹回到controllerA之前,你设置了标志

  1. self.delegate.jumpNeeded = YES;
  2. [self.navigationController popViewControllerAnimated:YES];

就是这样.你不必与controllerC做任何事情.还有一些其他的方法可以做,但这是很简单的,为您的需要.希望它适合你.

猜你在找的iOS相关文章