I'm new to iOS, my question is: What is the best way to pass data from one UIViewController to another, through singleton class or *.h file?
Asked
Active
Viewed 175 times
1
-
2Refer this :- http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers/22934200#22934200 – borncrazy Apr 09 '14 at 11:51
1 Answers
1
1) use properties to pass data.
- (void)someMethod {
MyViewController * vc = [[MyViewController alloc] initWithNibName:"MyViewController" bundle:nil];
vc.someData = data;
}
Also if you use Segues you should do it prepareForSegue
method
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
MyViewController *vc = segue.destinationViewController;
vc.someData = data;
}

Leandros
- 16,805
- 9
- 69
- 108

Kostiantyn Koval
- 8,407
- 1
- 45
- 58
-
-
you can pass a singleton this way if you want. vc.someData = [MyDataStorage sharedInstance]; – Kostiantyn Koval Apr 09 '14 at 12:23
-
you can read answer here http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers?lq=1 – Kostiantyn Koval Apr 09 '14 at 12:24
-
we can pass data through public property, then why we are using protocol – RameshKumar Selvam Jun 13 '14 at 09:37