【问题标题】:how to Pass values between views in popViewControllerAnimated?如何在 popViewController Animated 中的视图之间传递值?
【发布时间】:2011-07-16 14:07:48
【问题描述】:

我想知道如何在 popViewControllerAnimated 中的视图之间传递值。

这是我的场景: 我有一个包含表格视图的视图,在选择单元格时我们转到另一个视图,我需要在文本框中输入值,然后单击一个按钮返回到需要在表格视图单元格中显示文本框值的上一个视图。

我该怎么做?

这就是我所做的:

NewContact *nc = [[NewContact alloc] initWithNibName:@"NewContact" bundle:nil];
// ...
// Pass the selected object to the new view controller.
nc.name=[firstName text];
//[self.navigationController pushViewController:nc animated:YES];
[self.navigationController popViewControllerAnimated:YES];
[nc release];  

【问题讨论】:

  • 最专业的方式(可能有点过头了,但是在扩展应用时会很有帮助):创建一个合适的数据模型,让你的视图控制器直接访问模型对象。
  • 使用UIViewController的parentViewController属性引用现有的NewContact实例。
  • 嗨,deepak,你能解释一下吗
  • @user746909 看看我标记为重复的一个非常相似的问题。它应该解释解决这个问题的各种方法,包括我自己的。

标签: iphone objective-c cell tableview


【解决方案1】:

在 ViewControllerB 中,为 popViewControllerAnimated 声明委托并设置动作:

@interface ViewControllerB : UIViewController {
    id delegate;
}

@property (nonatomic, retain) id delegate;

@synthesize delegate;

- (id) init ... {
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                                              style:UIBarButtonItemStyleBordered
                                                                             target:self
                                                                             action:@selector(didBack:)] autorelease];
}

- (void) didBack:(id)sender {
    if ([delegate respondsToSelector:@selector(setProperty:)]) { 
        [delegate setProperty:property];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

在ViewControllerA中,提供设置本地属性和设置委托的函数:

ViewControllerB controllerB = [[ViewControllerB alloc] init...];
[controllerB setDelegate:self];
[self.navigationController pushViewController:controllerB animated:YES];
[controllerB release];

【讨论】:

    【解决方案2】:

    您需要将值存储在全局变量之一中,例如您可以在 appDelegate 文件中声明。请参阅this post

    如果您使用的是 UITextField,那么您可以在下面的委托方法中从 UITextField 将值存储在上面的变量中。

    - (BOOL)textFieldShouldReturn:(UITextField *)textField;
    

    希望对您有所帮助。

    【讨论】:

    • 你确定这是 OP 想要的吗?
    • @Deepak 根据他的描述是的。他想在 poptoViewController 时存储一些值。
    【解决方案3】:

    您可以为此使用NSNotificationCenter,将对象与调用一起传递。

    操作方法:Send and receive messages through NSNotificationCenter in Objective-C?

    【讨论】:

      【解决方案4】:

      根据您在评论中的要求,

      假设您从ViewControllerA 实例导航到ViewControllerB 实例并且您希望调用ViewControllerA 的方法,您可以这样做,

      ViewControllerA * viewControllerA = (ViewControllerA *)self.parentViewController.
      [viewController methodToCall];
      

      要使用它来解决问题中的要求,您可以使用属性。

      viewControllerA.name = firstName.text; // Bits from your code snippet
      

      但是,在表格视图中,我希望您有一个可变数组为数据源提供动力。你可以参考它,假设它是一个属性。

      [viewControllerA.dataSourceArray addObject:firstName.text];
      

      【讨论】:

      • 利用委托模式,可以避免耦合ViewControllerA和ViewControllerB,但是稍微复杂一点。
      猜你喜欢
      • 2015-07-13
      • 1970-01-01
      • 2013-03-09
      • 1970-01-01
      • 1970-01-01
      • 2010-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多