How to get tableview cell text label from 2nd viewcontroller to a label from 1st viewcontroller?

127 views Asked by At

I'm new iOS developer. I have 2 viewcontroller like 2 pictures below (I'll called its VC1 and VC2):
VC1: VC1

And VC2 is a list of printers: VC2

Now I want every time I selected a cell in VC2 then press right bar button in top right the text label of cell will send to VC1 and display instead of EP-806A label, I've searched on internet and found solution is write a delegate to passing data. But I don't know how to write it in my case. Can someone help me, please?

1

There are 1 answers

0
DaidoujiChen On BEST ANSWER

1st In VC2, you must create delegate

@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

2nd, add the delegate to VC1

@interface VC1 : UIViewController <VC2Delegate>
@end

3rd, remember set VC2.delegate to VC1

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

4th, implement changeToText: in VC1

@implementation VC1
- (void)changeToText:(NSString *)text {
    // change the text here
}
@end

wish it could help you.