简体   繁体   English

将 integer 值从一个 UIView 传递到另一个 UIview

[英]passing integer value from one UIView to another UIview

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

Take this in.h file in SecondViewController在 SecondViewController 中获取这个 in.h 文件

int value;

Make below function in SecondViewController在 SecondViewController 中制作下面的 function

-(void)setValue:(int)number{
    value=number;
}

Now In First view controller do like this:现在在第一个视图中 controller 这样做:

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.现在,在 secondViewController viewWillAppear 方法中写下这个。

-(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.如果你没有使用 navigationContoller 那么你可以做这样的事情。

SecondViewControler *objSecond = [[SecondViewController] initwithNibName:@"secondview.xib" bundle:nil];
[objSecond setValue:15]; // Pass actual Value here
[objSecond viewWillAppear:YES];
[self.view addSubview:objSecond];
[objSecond release];

declare a varable in.h file声明一个变量 in.h 文件

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

You can pass it as integer value itself as u do in c or in any other language.您可以将其作为 integer 值本身传递,就像您在 c 或任何其他语言中所做的那样。 In the second view, u can convert it to string with [NSString stringWithFormat:@"%d",intVal];在第二个视图中,您可以使用 [NSString stringWithFormat:@"%d",intVal] 将其转换为字符串;

In second view, declare a NSUInteger variable, say NSUInteger _intVal.在第二个视图中,声明一个 NSUInteger 变量,比如 NSUInteger _intVal。

In.h file, declare a method like在.h 文件中,声明一个类似的方法

-(void)setIntVal:(NSUInteger)inVal;

In.m file,在.m 文件中,

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

Now u can use the _intVal in the second view.现在您可以在第二个视图中使用 _intVal。

When you are going from view1 to view2 then do like this.当您从 view1 转到 view2 时,请这样做。

Take one variable in view2 and set the property as在 view2 中取一个变量并将属性设置为

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

in view2.m file do this.在 view2.m 文件中执行此操作。 @synthesize val;

Then when you are adding the view2 as subview to view1 then然后,当您将 view2 作为子视图添加到 view1 时

view2object.val=someval;// like 1,2,3....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM