使用Objective-C在视图之间传递数据

时间:2013-04-17 05:55:25

标签: objective-c

我正在使用 Objective-C 中的按钮点击创建一个我需要在视图之间传递数据的应用程序。

我创建了 ViewController.m 传递 DetailController.m 获取 ... < / p>

ViewController.m:

   - (IBAction)btnSubmit:(id)sender {

     NSString *name = [txtName text];

     DetailController *view2 = [[[DetailController alloc] 
     initWithNibName:nil  bundle:nil] autorelease];
     [self presentViewController:view2 animated:NO completion:NULL];

      }

DetailController.m

    - (void)viewDidLoad
     {
        [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.

        lblResult.text = [self.detailItem description];
      }

我无法理解为什么我无法将名称值传递给其他视图

2 个答案:

答案 0 :(得分:6)

忘了添加这一行:

view2.detailItem = name;

      (IBAction)btnSubmit:(id)sender {

         NSString *name = [txtName text];

            DetailController *view2 = [[[DetailController alloc] 
            initWithNibName:nil bundle:nil] autorelease];
            view2.detailItem = name;
            [self presentViewController:view2 animated:NO completion:NULL];

  }

答案 1 :(得分:1)

DetailController *view2 = [[[DetailController alloc] 
     initWithNibName:nil  bundle:nil] autorelease];
view2.detailItem = self.yourValue;    
[self presentViewController:view2 animated:NO completion:NULL];