2

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 );
Community
  • 1
  • 1
iUser
  • 1,075
  • 3
  • 20
  • 49

4 Answers4

3

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.

iOS
  • 3,526
  • 3
  • 37
  • 82
  • 1
    Ohh... Yes , It works just fine.. Thanks a lot Developer.. :) I was working on it from last 2 days.. Again thanks for help..!! :) – iUser Jul 27 '11 at 12:09
0

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;
Ilanchezhian
  • 17,426
  • 1
  • 53
  • 55
  • I tried this but don't know how to get that value in my secondView.. This is the main problem i am trying to solve.. – iUser Jul 27 '11 at 11:33
  • Ya.. I tried that but still value becomes 0(Zero) then i tried Developer's answer.. and it works fine.. I got my solution :) Thanks..!! – iUser Jul 27 '11 at 12:11
0

A simply way is, store the value in the NSUserDefault and get the value of the integer from NSUserDefault in secondviewcontroller.

iOS
  • 3,526
  • 3
  • 37
  • 82
  • Thanks for quick reply.. But I am new to iPhone development. So can you please tell me how to store value in NSUserDefault? do you have any link or document for that?? – iUser Jul 27 '11 at 11:41
  • Will send you in the next answer. – iOS Jul 27 '11 at 11:43
0

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];
Deeps
  • 4,399
  • 1
  • 26
  • 27