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

时间:2014-06-13 06:55:47

标签: ios objective-c ios7 uitextfield

我只是使用以下过程将数据从一个ViewController传递到另一个viewController

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

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

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

-(void)viewDidLoad:

{         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ThingYouWantToDoWithString :) name:@“sendString”object:nil]; }

- (void)ThingYouWantToDoWithString:(NSNotification *)notification{

    passedUserID = [notification object];
    NSLog("%@", passedUserID);

}

2 个答案:

答案 0 :(得分:1)

@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。

答案 1 :(得分:0)

我们可以使用属性

在视图控制器之间传递数据

考虑两个A类和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];

}