2013-04-23 53 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]; 

}