popViewController和pushController问题

时间:2012-10-29 09:37:11

标签: iphone ios5 pushviewcontroller popviewcontroller

我遇到了使用pop / pushViewControllers重定向viewControllers的问题。我有三个视图控制器,如下面的图像 -

Control Flow

在我的第3个视图中,基于ToggleButton我有一些toggleButton,第二个视图中的数据会发生变化。之后,在第3个视图上更改某些内容并在我的第3个视图中按Done按钮我刚刚使用pushViewController启动了我的第2个ViewController,并且更改成功发生。

但是,问题是,当我尝试按下第二个视图页面上的后退按钮时,它再次将我重定向到第三个viewController(因为pushViewController)

我尝试使用popViewController开始我的第二个视图,但是它给出了异常。

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Tried to pop to a view controller that doesn't exist.

我使用了以下代码 -

SecondViewController *second = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
second.array = resultArray;
second.indexValue = ind;
[self.navigationController popToViewController:second animated:YES];

我该如何解决这个问题?有什么想法吗?

4 个答案:

答案 0 :(得分:1)

从第3个viewcontroller你只需要调用这个方法来弹出第二个viewcontroller:

[self.navigationController popViewControllerAnimated:YES];

编辑:

要通知第二个vc进行数据更改,您可以使用委托。请遵循:

On ThirdViewController.h

@protocol DataChangeProtocol;
@interface ThirdViewController : UIViewController

@property(nonatomic,assign) id<DataChangeProtocol> delegate;

@end


@protocol DataChangeProtocol

- (void)thirdViewcontroller:(ThirdViewController*)vc dataChangedto:(NSDictionary *)changedData;

@end

在从secondVC推送thridVC之前,将secondVC指定为thirdVC的代表:

ThirdViewController *thirdVC = [[ThirdViewController alloc]init..];
thirdVC.delegate = self;
[self.navigationController pushViewController:thirdVC animated:YES];

并在SecondViewController.m中

- (void)thirdViewcontroller:(ThirdViewController*)vc dataChangedto:(NSDictionary *)changedData {
 // Change your values in the UI using the passed value from changedData.

}

答案 1 :(得分:1)

如果你想转到当前的前一个视图控制器,那么你必须这样做:

[self.navigationController popViewControllerAnimated:YES];

答案 2 :(得分:1)

您创建了我们的viewcontroller:

SecondViewController *second = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

然后你想从你的堆栈中弹出它,但是这个控制器不在堆栈中,因为你在第三个viewController中创建它。

答案 3 :(得分:1)

    //it used in first class
     SecondViewController *second = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
second.array = resultArray;
second.indexValue = ind;
    [self.navigationController pushViewController:second animated:YES];
    //it move to second class

    //it used in second class
    [self.navigationController popViewControllerAnimated:YES];
    //it move to firsr class