我有 2 个 ViewController,第一个是 TableView,第二个是带有标签的按钮。当我单击第二个 ViewController 中的按钮时,我需要返回 TableView 并在
中设置cell.detailTextLabel.text
按钮上标签的文本。
要返回到第一个 View ,我使用:
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
但是我如何将第二个 View 中的标签设置为:
cell.detailTextLabel.text
第一眼?????
最佳答案
我会在第二个 View Controller 中定义一个协议(protocol)和委托(delegate)
@protocol SecondViewController;
@interface SecondViewController : UIViewController
@property (nonatomic, assign) id<SecondViewController> delegate;
@end
@protocol SecondViewController <NSObject>
- (void)secondViewController:(SecondViewController *)controller didTappedOnButton:(UIButton *)button;
@end
然后当点击按钮时调用委托(delegate):
- (IBAction)buttonTapped:(UIButton *)sender
{
// do somthing..
// then tell the delegate about the button tapped
[self.delegate secondViewController:self didTappedOnButton:sender];
}
在你的第一个 View Controller 中实现协议(protocol)
@interface FirstViewController : UIViewController <SecondViewControllerDelegate>
当你推送第二个 View Controller 时,将第一个设置为第二个委托(delegate):
- (void)someMethodThatPushTheSecondViewController
{
SecondViewController *svc = [[SecondViewController alloc] init];
[self.navigationController pushViewController:svc animated:YES];
svc.delegate = self;
}
并实现委托(delegate)方法在点击按钮时得到通知
- (void)secondViewController:(SecondViewController *)controller didTappedOnButton:(UIButton *)button
{
// do somthing after button tapped
// you can get the button title from button.titleLabel.text
}
关于iphone - 从第二个 ViewController 到第一个 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11900202/