popToRoot时如何传递值

时间:2012-04-13 11:22:44

标签: iphone ios xcode uinavigationcontroller

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

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

2 个答案:

答案 0 :(得分: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];

答案 1 :(得分:2)

只需使用以下代码

即可
NSArray *arr = [self.navigationController viewControllers];
CLASS_OF_ROOT_VIEW_CONTROLLER *rvc = (CLASS_OF_ROOT_VIEW_CONTROLLER *)[arr objectAtIndex:0];
rvc.variable = value;