将一个整数值从一个ViewController传递到另一个ViewControl,并将其显示在UITextField中



我刚刚使用下面的进程将数据从一个ViewController传递到另一个ViewController

我想在SecondViewController的UITextField中显示数据。传递的值必须是int值。

[[NSNotificationCenter defaultCenter] postNotificationName:@"sendString" object:userID];

更改要获取字符串的viewDidload方法。

-(void)viewDidLoad:

{[[NSNotificationCenter defaultCenter]addObserver:自选择器:@selector(ThingYouWantToDoWithString:(名称:@"sendString"对象:nil];}

- (void)ThingYouWantToDoWithString:(NSNotification *)notification{
    passedUserID = [notification object];
    NSLog("%@", passedUserID);

}

@property(nonatomic,assign) NSInteger userid;

在两个视图控制器中

然后

SecondViewController *svc=[[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
 svc.passedUserID=userId;
[self.navigationController pushViewController:svc animated:YES];
NSLog(@"%@",passedUsrID);

别忘了提到%d表示整数。

我们可以使用属性在视图控制器之间传递数据

考虑两个类别A&B、 如果我们需要将数据从A传输到B

在B 中写入属性

@property(nonatomic,retain) NSString *userid;

并且在推动B 时

B *b = [[B alloc] init];
b.userid = INTERGER_YOU_NEED_TO_PASS;
[self.navigationController pushViewController:b animated:YES];

和B.m

 -(void)viewDidLoad
 {
    aTextField.text = self.userid;
 }

如果你想使用通知中心,你已经做了以上

按照更改代码行

- (void)ThingYouWantToDoWithString:(NSNotification *)notification{
    passedUserID = [notification object];
    NSLog("%@", passedUserID);
    yourTextField.text = [NSString stringWithFormat: @"%@",passedUserID];
}

相关内容