簡體   English   中英

如何快速顯示第二個UIViewController中的UILabel上的UITextField數據?

[英]How do I show UITextField data on a UILabel in a second UIViewController in swift?

我想將在當前視圖控制器中輸入UITextFields的數據傳遞到保存按鈕操作中的第二個視圖控制器。 我正在使用情節提要,有人可以建議我如何實現此目的嗎? 我正在使用Xcode 6。

@IBAction func SAVEButtonTapped(sender: AnyObject) {

    let viewcontroller = SecondViewController();
    viewcontroller.name = self.nametextfield.text;
    viewcontroller.phone = self.phonetextfield.text;
    viewcontroller.city = self.citytextfield.text;

}

我發現我的問題

@IBAction func SAVEButtonTapped(sender: AnyObject) {

let viewcontroller = SecondViewController();
viewcontroller.name = self.nametextfield.text;
viewcontroller.phone = self.phonetextfield.text;
viewcontroller.city = self.citytextfield.text;

}

這行的問題讓viewcontroller = SecondViewController()

我將其更改為

讓viewcontroller = self.storyboard!.instantiateViewControllerWithIdentifier(“ SecondViewController”)為! SecondviewController

通過此更改,我的代碼可以正常工作,謝謝..

FirstViewController嘗試FirstViewController

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    UIViewController *destinationVC = [segue destinationViewController];
    If ([destinationVC isKindOfClass:[SecondViewController class]]) {

        SecondViewController *viewcontroller = (SecondViewController *)destinationVC;
        viewcontroller.name = self.nametextfield.text;
        viewcontroller.phone = self.phonetextfield.text;
        viewcontroller.city = self.citytextfield.text;
    }
}

假設您僅對SecondViewController有一個SecondViewController 如果對SecondViewController有多個segue,則可以檢查segue標識符,而不是isKindOfClass檢查。

您正在執行的操作將無法正常工作,因為在執行viewDidLoad:之前,不會初始化您的視圖。 基本上,您是將值分配給nil個對象。

您要么需要添加字符串屬性,然后再將其分配給viewDidLoad的視圖元素,要么如果您在過渡中使用segue,請執行Akhilrajtr提到的操作。

在VC之間傳遞數據創建目標VC的意圖,如下所示

SecondViewController *controller = [[SecondViewController alloc]  initWithNibName:@"SecondViewController" bundle:nil];
    viewcontroller.name = self.nametextfield.text;
    viewcontroller.phone = self.phonetextfield.text;
    viewcontroller.city = self.citytextfield.text;
 [self.navigationController pushViewController:controller animated:YES];

暫無
暫無

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

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