【问题标题】:how to pass value when popToRootpopToRoot时如何传递值
【发布时间】:2012-04-13 11:22:43
【问题描述】:

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

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

【问题讨论】:

  • 学习使用委托...会很有帮助的。
  • 委托什么时候失效了?!
  • 整个 iPhone sdk 是基于委托的……你怎么能说它不适用于 ios5……任何书面证明你也可以指导我……? @AalokParikh
  • 但它不像 ios4 那样工作
  • @AalokParikh 代表团仍然有效,但方式略有不同,请参见此处.. stackoverflow.com/questions/6529191/…

标签: iphone ios xcode uinavigationcontroller


【解决方案1】:

对于要从中弹出的 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 中使其符合该协议,并实现解除方法 -

-(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];

【讨论】:

    【解决方案2】:

    只需使用下面的代码

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-18
      • 2016-07-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多