2012-08-10 122 views
2

我有2个ViewControllers,在1st - TableView和2nd - 按钮上有标签。当我点击在第二的ViewController的按钮,我需要回去上的TableView,并设置在从第二个ViewController到第一个ViewController

cell.detailTextLabel.text 

文本从标签上的按钮。

对于回到第一种观点我用:

[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES]; 

,但我怎么可以从第二视图设置标签:在第一种观点

cell.detailTextLabel.text 

?????

+0

你无法得到通知。改为使用'viewWillAppear'方法。 – Martol1ni 2012-08-10 10:57:07

+0

即使在viewWillAppear中,您也无法执行它,而无需执行协议 – 2012-08-10 12:53:10

+0

@TeodorCarstea虽然这基本上是真的,但主要的回收信息是,当活动视图控制器要触发视图中已收到'viewDidDisappear ',无论您用于更新的机制是什么,都不应直接更新非活动视图(该视图可能在发生'didReceiveMemoryWarning'时发布)。您必须更新您的模型,并且只有当另一个视图控制器变为活动状态并且接收到'viewWillAppear'时,才应该执行UI更改。 – Rob 2012-08-11 01:11:47

回答

2

当按钮被点击我将在第二视图控制器定义一个协议&委托

@protocol SecondViewController; 

@interface SecondViewController : UIViewController 

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

@end 


@protocol SecondViewController <NSObject> 
- (void)secondViewController:(SecondViewController *)controller didTappedOnButton:(UIButton *)button; 
@end 

然后调用该委托:

- (IBAction)buttonTapped:(UIButton *)sender 
{ 
    // do somthing.. 

    // then tell the delegate about the button tapped 
    [self.delegate secondViewController:self didTappedOnButton:sender]; 
} 

在您的第一视图控制器实现协议

@interface FirstViewController : UIViewController <SecondViewControllerDelegate> 

当您按下第二个视图控制器时,设置第一作为第二委托:

- (void)someMethodThatPushTheSecondViewController 
{ 
    SecondViewController *svc = [[SecondViewController alloc] init]; 
    [self.navigationController pushViewController:svc animated:YES]; 
    svc.delegate = self; 
} 

并实现委托方法时,按键敲击

- (void)secondViewController:(SecondViewController *)controller didTappedOnButton:(UIButton *)button 
{ 
    // do somthing after button tapped 
    // you can get the button title from button.titleLabel.text 
} 
+0

哇!谢谢你的详细回答!)))))你可以给我你的Skype吗?)这不是关于通信的问题) – rubik 2012-08-10 12:22:27

0

要访问父类的方法或属性,你必须实现一个协议,并使用它的委托。您可以使用您在当前(父级)类中创建的类对象来访问子类方法/属性。但是,您希望如何从子类访问父类实体?是的,执行协议。

或者新手方法:点击按钮后,将需要的值保存到NSUserDefaults中。然后,当你到你的父类(viewController 1),离开viewWillAppear,检查保存的值,如果它不是零,则显示它。

0
[self.navigationController popViewControllerAnimated:YES]; 
相关问题