2013-04-23 55 views
0

这种方法对我没有用。我正在工作几分钟。我以某种方式不能。iOS:传递数据错误

这是我的代码。

- (IBAction)donebutton:(id)sender { 
    AddTaskViewController *addtask = [[AddTaskViewController alloc]initWithNibName:@"AddTask" bundle:nil]; 
    addtask.testlabel.text = self.zaman1.text; 
    [self dismissViewControllerAnimated:YES completion:nil]; 

} 

一切正常,但无法正常工作。那不是吗?这是错误的?

+2

你期望发生的?在您创建视图控制器的那一刻,然后对它完全没有任何影响。你是否想要'presentViewController:...'? – Tommy 2013-04-23 23:37:12

+0

我想等于label.text – Salieh 2013-04-23 23:42:36

+0

@Salieh:Tommy说得对,可能是你用'presentViewController'混淆了它。你究竟想达到什么目的,你能否详细说明一下? – 2013-04-24 00:01:44

回答

1

您必须在您的viewWillAppear方法中分配此字符串,IBOutlets(我假设testlabel是IBOutlet UILabel *)只能在视图初始化之前配置。如果这没有帮助,请说明错误是什么。

+0

@property(弱,非原子)IBOutlet UILabel * testlabel; testlabel软弱无力。而不是工作。我该怎么办?我不明白 – Salieh 2013-04-23 23:52:16

+1

或者,至少在'viewDidLoad'中。 – 2013-04-23 23:54:25

+0

我该怎么做?我怎样才能添加到'viewDidLoad' – Salieh 2013-04-23 23:56:32

0

更好的方法是在AddTaskViewController上创建NSString*属性。你可以这样说:

AddTaskViewController.h添加以下内容:

@property (nonatomic, strong) NSString* myLabelsText; 

然后在AddTaskViewController.m一定要添加这个在viewWillAppear

self.testlabel.text = self.myLabelsText; 

现在假设你已经声明了testLabelmyLabelsText适当,他们得到合成,你的视图控制器将在适当的时间正确应用字符串,然后你的功能应该改为:

- (IBAction)donebutton:(id)sender { 
    AddTaskViewController *addtask = [[AddTaskViewController alloc]initWithNibName:@"AddTask" bundle:nil]; 

    // Set the value on your new NSString* property and let the view controller handle the rest 
    addTask.myLabelsText = self.zaman1.text; 

    // Don't you want to 'present' the view controller rather than 'dismiss' after having provided it with data? 
    [self dismissViewControllerAnimated:YES completion:nil]; 

}