如何回到根视图控制器,但后来推到不同的视图? [英] How to pop back to root view controller but then push to a different view?

查看:179
本文介绍了如何回到根视图控制器,但后来推到不同的视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简单的应用程序,有3个视图控制器。根视图控制器是项列表,基本表视图。关闭此视图控制器,我基于一些用户交互推送两个不同的视图控制器 - 创建项目视图控制器或查看项目 view controller。

I am writing a simple application that has 3 view controllers. The root view controller is an item listing, basic table view. Off of this view controller, I push two different view controllers based on some user interaction - a create item view controller or a view item view controller.

因此,故事板只是看起来像一个V或者某物。

So, the storyboard segues just look like a V, or something.

在我的创建项目视图控制器,我想它回弹到根视图控制器,当用户创建一个新的项目,但随后推到查看项控制器,以便我可以查看新创建的项。

On my create item view controller, I would like it to pop back to the root view controller when the user creates a new item, but then push to the view item controller so that I can look at the newly created item.

我似乎无法使此工作。这很容易弹回到根视图控制器,但我无法推送查看项目控制器。

I can't seem to get this to work. It's easy enough to pop back to the root view controller, but I'm unable to push that view item controller.

任何想法?我粘贴我的代码,下面。

Any ideas? I've pasted my code, below. The pop function works, but the new view never appears.

- (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方法构建一些简单的逻辑,并使用委托回调来翻转逻辑开关。基本上是对根控制器的后向引用。这里是一个快速示例。

An easy way to accomplish what you want to do is to build some simple logic into your main root view controllers -(void)viewWillAppear method and use a delegate callback to flip the logic switch. basically a "back reference" to the root controller. here is a quick example.

主根控制器(考虑这个控制器a) - 好调用它controllerA
设置一个属性来跟踪跳转状态

main root controller (consider this controller a) - well call it controllerA set a property to keep track of the jump status

@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中push到controllerB时有select方法

Now, in your main root controller,when a tableview row is selected do something like this when pushing to controllerB in your tableView did select method

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

然后实现你的segue方法准备

then implement your prepare for segue method

- (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的属性来保存后面的引用
,你需要导入头文件根控制器

Now move on to controllerB you need to set a property called "delegate" to hold the back reference and you need to import the header file from the root controller

#import "controllerA"

@property (nonatomic,weak) controllerA *delegate;

,然后在弹回controllerA之前,您设置标志

then just before you pop back to controllerA, you set the flag

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

就是这样。你不必对controllerC做任何事情。还有几个其他的方法可以做,但这是非常直接的你的需要。希望它为你工作。

and that is about it. You don't have to do anything with controllerC. There are a few other ways to do, but this is pretty straight forward for your needs. hope it works out for you.

这篇关于如何回到根视图控制器,但后来推到不同的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆