0

I am creating a code in which i want to pass integer value for one uiview to another view.In another uiview that integer value so as text of label.How make code for that?

ram
  • 1,193
  • 1
  • 15
  • 27

4 Answers4

1

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
0

declare a varable in .h file

NSInteger tempValue;

declare a method like that:

- (void)SetValue:(NSInteger)value {
    tempValue=value;
}

- (void)GetValue{
   return tempValue;
}

when you set it, you use:

AppDelegate* app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[app SetValue:xxxx]

when you need it, use:

AppDelegate* app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
xxxx = [app GetValue];
Bonny
  • 2,038
  • 1
  • 14
  • 15
0

You can pass it as integer value itself as u do in c or in any other language. In the second view, u can convert it to string with [NSString stringWithFormat:@"%d",intVal];

In second view, declare a NSUInteger variable , say NSUInteger _intVal.

In .h file, declare a method like

-(void)setIntVal:(NSUInteger)inVal;

In .m file,

-(void)setIntVal:(NSUInteger)inVal
{
   _intVal = inVal;
}

Now u can use the _intVal in the second view.

Ilanchezhian
  • 17,426
  • 1
  • 53
  • 55
0

When you are going from view1 to view2 then do like this.

Take one variable in view2 and set the property as

@property (nonatomic,readwrite) int val;  //this is in the view2 .h file

in view2 .m file do this. @synthesize val;

Then when you are adding the view2 as subview to view1 then

view2object.val=someval;// like 1,2,3....
Kundan
  • 3,084
  • 2
  • 28
  • 65
Tendulkar
  • 5,550
  • 2
  • 27
  • 53