iphone - 从第二个 ViewController 到第一个 ViewController

标签 iphone objective-c ios uitableview uiviewcontroller

我有 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/

相关文章:

iphone - 为 UITableView 排序 JSON NSDictionary

iphone - 是否可以在不打开特定 View 的情况下拍摄屏幕截图?

iphone - UICollectionView:像 Safari 选项卡或 App Store 搜索一样的分页

android - 如何在更新的 ionic 3 的移动应用程序中获取图像的自动幻灯片旋转?

ios - 使用 TestFlight 和 Hockeyapp (iOS) 等服务测试 MDM

ios - CLGeocoder:美国州名将以短代码形式出现

objective-c - Objective-C 中的三元运算符赋值

ios - products 文件夹下 app.xctest 为红色正常吗?

ios - 如何将 NSDate 对象设置为午夜?

iphone - 添加一个 UIButton 到 tableView headerView