【问题标题】:Pass an integer value from one ViewController to another ViewController and display it in UITextField将整数值从一个 ViewController 传递到另一个 ViewController 并在 UITextField 中显示
【发布时间】:2014-08-03 15:55:22
【问题描述】:

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

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

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

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

-(void)viewDidLoad:

{ [[NSNotificationCenter defaultCenter] addObserver:self 选择器:@selector(ThingYouWantToDoWithString:) name:@"sendString" object:nil]; }

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

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

}

【问题讨论】:

    标签: ios objective-c ios7 uitextfield


    【解决方案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。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 我收到此错误:具有“保留(或强)”属性的属性必须是对象类型
      猜你喜欢
      • 1970-01-01
      • 2014-07-25
      • 1970-01-01
      • 2014-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多