如何从第二个viewcontroller获取tableview单元格文本标签到第一个viewcontroller的标签?

时间:2015-06-17 08:20:28

标签: ios uitableview delegates nsnotifications

我是新的iOS开发人员。 我有2个视图控制器,如下图2(我称之为VC1和VC2):
VC1: VC1

VC2是打印机列表: VC2

现在我希望每次在VC2中选择一个单元格,然后按右上角的右侧栏按钮,单元格的文本标签将发送到VC1并显示而不是 EP-806A 标签,我在互联网上搜索,发现解决方案是写一个传递数据的委托。但我不知道怎么写我的情况。请有人帮帮我吗?

1 个答案:

答案 0 :(得分:1)

1st在VC2中,您必须创建委托

@protocol VC2Delegate;
@interface VC2 : UIViewController
@property (nonatomic, weak) id <VC2Delegate> delegate;
@end

@protocol VC2Delegate <NSObject>
@required
- (void)changeToText:(NSString *)text;
@end

@implementation VC2
// this is your "完了" action
- (IBAction)doneAction:(id)sender {
   ...
   [self.delegate changeToText:@"what you want"];
}
@end

2,将委托添加到VC1

@interface VC1 : UIViewController <VC2Delegate>
@end

3,记得将VC2.delegate设置为VC1

VC2 *vc2 = [VC2 new];
vc2.delegate = self;
[self.navigationController pushViewController:vc2 animated:YES];

第4,在VC1中实施changeToText:

@implementation VC1
- (void)changeToText:(NSString *)text {
    // change the text here
}
@end
希望它可以帮到你。

相关问题