I am creating an iphone app in which i have some vale in integer in my oneviewcontroller and i want that integer value in my secondviewcontroller controller.
value will be random. I stored this integer value in id score
Nw I am using this code but,i cant get integer value.
CODE : passing integer value from one UIView to another UIview
Integer Value always becomes zero at the secondviewcontroller.
Any Help will be appreciated. Thanks.MY CODE:
FirstView.h file
NSInteger myScore;
id score;
NSInteger totalscore;
.m file
NSInteger 开发者_如何转开发*cal = (myScore * 100)/400 ;
totalscore = cal;
score = totalscore;
SecondView.h file
int scorevalue ;
SecondView.m file
FirstViewController *firstVC = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
value = firstVC.Score;
NSLog(@"SCORE : %d" ,value );
Example:
If I need to store the int value 4 in FirstViewController,
[[NSUserDefaults standardUserDefaults] setObject:4 forKey:@"integer"];
[[NSUserDefaults standardUserDefaults] synchronize];
I can get the get in the SecondViewController as,
int value = [[NSUserDefaults standardUserDefaults] objectForKey:@"integer"]
NSLog(@"integer = %d", [[NSUserDefaults standardUserDefaults] objectForKey:@"integer"]);
Hope it will work perfectly.
A simply way is, store the value in the NSUserDefault and get the value of the integer from NSUserDefault in secondviewcontroller.
Since 'id' denotes objects (see ref) , but we require data type.
Instead of declaring it as id
, declare it as int
itself.
Something like,
int score;
or
NSInteger score;
Edit:
Also add the property and synthesize it. In .h
@property(nonatomic,assign) NSInteger score;
In .m
@synthesize score;
In the first view controller, pass the value to the secondView by,
NSInteger *cal = (myScore * 100)/400 ;
totalscore = cal;
score = totalscore;
secondViewObj.score = score;
Take this in .h file in SecondViewController
int value;
Make below function in SecondViewController
-(void)setValue:(int)number{
value=number;
}
Now In First view controller do like this:
ParentViewController *objSecond = [[ParentViewController] initwithNibName:@"parentView.xib" bundle:nil];
[objSecond setValue:15]; // Pass actual Value here
[self.navigationController pushViewController:objSecond animated:YES];
[objSecond release];
Now, In secondViewController viewWillAppear Method write this.
-(void)viewWillAppear:(BOOL)animated{
myValue = value;
}
Please check spelling mistakes as I hand written this. Hope this help.
If you are not using navigationContoller then you can do something like this.
SecondViewControler *objSecond = [[SecondViewController] initwithNibName:@"secondview.xib" bundle:nil];
[objSecond setValue:15]; // Pass actual Value here
[objSecond viewWillAppear:YES];
[self.view addSubview:objSecond];
[objSecond release];
精彩评论