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

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

问题描述

我正在编写一个具有 3 个视图控制器的简单应用程序.根视图控制器是一个item Listing,基本的表格视图.在这个视图控制器之外,我基于一些用户交互推送了两个不同的视图控制器 - create item 视图控制器或 view item 视图控制器.

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.

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

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.

我似乎无法让它工作.弹回根视图控制器很容易,但我无法推送该 view 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.

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

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行时,请执行此操作在您的 tableView 中推送到 controllerB 时确实选择了方法

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
}

现在转到控制器 B您需要设置一个名为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;

然后在您弹回控制器A之前,设置标志

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

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

就是这样.您不必对控制器C 做任何事情.还有其他一些方法可以做,但这对于您的需求来说非常简单.希望它对你有用.

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天全站免登陆