簡體   English   中英

將文本值從一個視圖傳遞到另一個視圖

[英]Passing text value from one view to another

我在表視圖控制器中有此控件,我想將單元格的文本值傳遞給模態視圖中的UITextField。

- (void)buttonTapped:(id)sender {
    AddName *addName = [[AddName alloc] init];
    UITableViewCell *cell = (UITableViewCell*)[sender superview];
    NSLog(@"text in buttontapped: %@", cell.textLabel.text);
    addName.nameField.text = cell.textLabel.text;
    addName.delegate = self;
    [self presentModalViewController:addName animated:YES];
}

NSLog顯示正確的單元格文本值。

在我的模態視圖的viewDidLoad方法上,我有這個...而且文本值從未設置過...

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"text: %@",nameField.text);
    nameField.autocapitalizationType = UITextAutocapitalizationTypeWords;
    nameField.autocorrectionType = UITextAutocorrectionTypeDefault;
    [nameField becomeFirstResponder];
}

這是怎么回事?

謝謝。

好像您假設模態視圖控制器已經加載了它的視圖,因為我假設nameField是一個子視圖。 但很明顯,在你的代碼試圖設置其點text屬性,文本字段及其父視圖還沒有被加載呢,所以你發送-setText:消息nil

而是將一個屬性添加到類型為NSString *模態視圖控制器中,然后在buttonTapped實現中進行設置。 然后,在模態視圖控制器的-viewWillAppear方法中,您可以從屬性中獲取值並將其放入文本字​​段。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM