1

想知道当我 popToRoot 时如何将值传递回根视图控制器。

introVideoViewController *intro = [introVideoViewController alloc];
    intro.fromReset =1;
    [self.navigationController popToRootViewControllerAnimated:NO];
4

2 回答 2

3

对于要从​​中弹出的 VC,您需要给它一个委托属性 -

@class MyViewController;

@protocol MyViewControllerDelegate <NSObject>

    -(void)myViewControllerDidDismiss:(MyViewController *)controller withSomeObject:(id)someObject;

@end


@interface MyViewController : UIViewController

@property (nonatomic, assign) id<MyViewControllerDelegate> myViewControllerDelegate;

@end

...并在根VC中使其符合该协议,并实现dismiss方法-

-(void)myViewControllerDidDismiss:(MyViewController *)controller withSomeObject:(id)someObject {

    // now I've got the object from the VC I just popped

}

忘了提到你需要调用 myViewControllerDidDismiss:withSomeObject: 当你弹出 VC 时。

编辑 - 还忘了提到你需要在创建它时将 VC 的委托设置为你的根 VC,否则它会在你弹回时尝试调用 nil -

[myViewController setMyViewControllerDelegate:self];
于 2012-04-13T11:31:53.787 回答
2

只需使用以下代码

NSArray *arr = [self.navigationController viewControllers];
CLASS_OF_ROOT_VIEW_CONTROLLER *rvc = (CLASS_OF_ROOT_VIEW_CONTROLLER *)[arr objectAtIndex:0];
rvc.variable = value;
于 2012-04-13T11:32:06.333 回答