Reputation: 11
I am working on an app in which I want to pass an object of a class from one UIViewController to another, How I can do that?
Upvotes: 1
Views: 1388
Reputation: 8243
when you initialize your new View Controller define a variable as follows:
MyClass *myClass = [[MyClass alloc] init];
myClass.username = @"Rahul Kushwaha";
// assume a property called username is defined in MyClass of a NSString type.
NextViewController *controller = [NextViewController alloc];
controller.myClassObject = myClass ;
[self.navigationController pushViewController:controller animated:YES];
Don't forget you have to define an object of type (MyClass) in NextViewController.
Sample
NextViewController.h
#import "MyClass.h"
@interface NextViewController
{
MyClass *myClassObject;
}
@property (nonatomic,retain) MyClass *myClassObject;
NextViewController.m
@synthesize myClassObject;
Upvotes: 7
Reputation: 6132
Say you have a firstViewController
and a secondViewController
.
Say, you want to pass on a NSString *testString;
from the first to the second view.
In that case, you should declare this NSString using @property
in the secondViewController.h-file, and @synthesize
it in the secondViewController.m-file.
In the firstViewController, when you create an instance of the secondViewController (through secondViewController *secondView = [[secondViewController alloc] initWith...];
, use the line: secondView.stringYouCreatedUsingSynthesize = testString;
Upvotes: 0