0

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?

Bad_Developer
  • 537
  • 5
  • 20
  • 1
    I think no need for delegate here, just write one singleton class which can stored your data when you selected any cell and access that data in another view. – Pravin Tate Jun 17 '15 at 08:36
  • @PravinTate: you mean I can store it into NSUserdefault instead of using delegate, right? – Bad_Developer Jun 17 '15 at 09:38
  • No, don't use NSUserdefault just make one single class as singleton and stored data in that. just search what is mean by singleton class and how to use it. so you will get all idea about how to solve your problem – Pravin Tate Jun 17 '15 at 09:44
  • possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – asjj Jun 17 '15 at 10:04

1 Answers1

1

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.

DaidoujiChen
  • 146
  • 8