方法一:
RootViewController
-(IBAction)nextPage{
int antalratt = 12; // Value to be transfered
FirstViewController * fvc = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
fvc.answer = antalratt;
[self presentModalViewController:fvc animated:YES];
[fvc release];
}
FirstViewController
@interface FirstViewController : UIViewController
{
int answer;
}
@property(nonatomic,assign) int answer;
@implementation FirstViewController
@synthesize answer;
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"%d",answer); // //displays answer on log
}
@end
方法二(AppDelegate)
AppDelegate
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
int antalratt;
}
@property(nonatomic ,assign) int antalratt;
RootViewController
-(IBAction)nextPage{
int antalratt = 12; // Value to be transfered
AppDelegate * delegate = [[UIApplication sharedApplication] delegate];
delegate.antalratt = antalratt;
FirstViewController * fvc = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
[self presentModalViewController:fvc animated:YES];
[fvc release];
}
FirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
AppDelegate * delegate = [[UIApplication sharedApplication] delegate];
NSLog(@"%d",delegate.antalratt); //displays answer on log
}
方法 3 (NSUserDefaults)
RootViewController
-(IBAction)nextPage{
int antalratt = 12; // Value to be transfered
[[NSUserDefaults standardUserDefaults] setInteger:antalratt forKey:@"answer"];
FirstViewController * fvc = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
[self presentModalViewController:fvc animated:YES];
[fvc release]; }
FirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
int ans = [[[NSUserDefaults standardUserDefaults] objectForKey:@"answer"] intValue];
NSLog(@"%d",ans); //displays answer on log
}