passing integer value from one UIView to another U

2019-06-14 05:30发布

问题:

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?

回答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];


回答2:

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.



回答3:

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];


回答4:

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....