2017-03-05 57 views
0

我有兩個viewcontroller類。 MainViewController有一個UITextField,SecondViewController有一個UILabel。我想從UILabe中的UITextField打印文本。請告訴我以編程方式執行此操作的最佳方式。下面是我的代碼:以編程方式在視圖控制器之間傳遞數據Swift

// AppDelegate.swift 

import UIKit 

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

    var window: UIWindow? 


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 

窗口=一個UIWindow(幀:UIScreen.main.bounds)

window?.makeKeyAndVisible() 

window?.rootViewController = MainViewController() 

window?.backgroundColor = UIColor.yellow 

application.statusBarStyle = .lightContent 


return true 
    } 

} 

// MainViewController類

import UIKit 

class MainViewController: UIViewController { 


    let label = UILabel() 

    let textField = UITextField() 



    override func viewDidLoad() { 

    super.viewDidLoad() 


    view.backgroundColor = UIColor.gray 
    setupLabel() 
    setupTextField() 
    setupButton() 
    } 

    func setupLabel() { 

    label.frame = CGRect(x: 40, y: 80, width: 300, height: 60) 
    label.text = "welcome to my world" 
    label.textColor = UIColor.yellow 
    label.font = UIFont.boldSystemFont(ofSize: 25) 
    label.textAlignment = .center 
    label.layer.borderWidth = 2 
    label.layer.borderColor = UIColor.yellow.cgColor 
    label.layer.cornerRadius = 5 
    view.addSubview(label) 
    } 

    func setupTextField() { 

    textField.frame = CGRect(x: 10, y: 200, width: self.view.frame.size.width - 20, height: 60) 
    textField.placeholder = "text here" 
    textField.textAlignment = .center 
    textField.font = UIFont.systemFont(ofSize: 25) 
    textField.layer.borderWidth = 2 
    textField.layer.borderColor = UIColor.yellow.cgColor 
    textField.layer.cornerRadius = 5 
    view.addSubview(textField) 
    } 

    func setupButton() { 

    let button = UIButton() 
    button.frame = CGRect(x: 50, y: 300, width: self.view.frame.size.width - 100, height: 60) 
    button.setTitle("Enter", for: .normal) 
    button.setTitleColor(UIColor.yellow, for: .normal) 
    button.layer.borderWidth = 2 
    button.layer.borderColor = UIColor.yellow.cgColor 
    button.layer.cornerRadius = 5 
    button.addTarget(self, action: #selector(buttonTarget), for: .touchUpInside) 
    view.addSubview(button) 
    } 

    func buttonTarget() { 

    // i missed here maybe 
    } 

} 

// SecondViewController類

import UIKit 

class SecondViewController: UIViewController { 


    let secondLabel = UILabel() 

    override func viewDidLoad() { 

    super.viewDidLoad() 

    setupLabelSecond() 
    } 


    func setupLabelSecond() { 

    secondLabel.frame = CGRect(x: 40, y: 80, width: 300, height: 60) 
    secondLabel.text = "this is Second Page" 
    secondLabel.textColor = UIColor.yellow 
    secondLabel.font = UIFont.boldSystemFont(ofSize: 25) 
    secondLabel.textAlignment = .center 
    secondLabel.layer.borderWidth = 2 
    secondLabel.layer.borderColor = UIColor.yellow.cgColor 
    secondLabel.layer.cornerRadius = 5 
    view.addSubview(secondLabel) 
    } 



} 
+0

@vadian你能回答他請或一些提示 –

+0

@vadian我試圖做沒有故事板。 –

+0

也有沒有故事板的建議。 – vadian

回答

4

你需要遵循一些步驟

第一步

最初嵌入與您最初的VC到導航控制器,用於例如

現在選擇故事板的第一控制器和點擊編輯>在嵌入...>導航控制器。

enter image description here

第二步

現在,你必須在第二控制器在故事板與新SecondViewController.swift文件鏈接。

選擇位於控制器頂部的黃色圓圈,點擊確定檢查面板圖標上的XCode窗口的右側,並鍵入類新.swift文件和StoryboardID領域

的名字

for e。克

enter image description here

步驟3

傳遞字符串

現在選擇故事板的其他控制器和下方添加SecondViewController類聲明此變量權:

class SecondViewController: UIViewController { 


let secondLabel = UILabel() 

    var stringPassed = "" 

使應用程序分配該變量的值與在viewDidLoad中下面的行的代碼()方法secondLabel

步驟4

在您的第一VC,內部按鈕

func buttonTarget() { 

    let myVC = storyboard?.instantiateViewControllerWithIdentifier("SecondVC") as! SecondViewController 
    myVC.stringPassed = label.text! 

    navigationController?.pushViewController(myVC, animated: true) 

} 

最後你得到了

func setupLabelSecond() { 

    secondLabel.frame = CGRect(x: 40, y: 80, width: 300, height: 60) 

    if let outputText = stringPassed 
    { 
    secondLabel.text = outputText 
    }else 
    { 
     secondLabel.text = "this is Second Page" 
    } 
    secondLabel.textColor = UIColor.yellow 
    secondLabel.font = UIFont.boldSystemFont(ofSize: 25) 
    secondLabel.textAlignment = .center 
    secondLabel.layer.borderWidth = 2 
    secondLabel.layer.borderColor = UIColor.yellow.cgColor 
    secondLabel.layer.cornerRadius = 5 
    view.addSubview(secondLabel) 
    } 

樣品就可以得到教程here

爲廈門國際銀行更新

func buttonTarget() { 

    var vcPass = SecondViewController(nibName: "SecondViewController", bundle: nil) 
    vcPass.stringPassed = label.text! 
    self.navigationController?.pushViewController(vcPass, animated: true) 


} 

更新不XIB和Storboard

改變你的appdelegate

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    self.window = UIWindow(frame: UIScreen.main.bounds) 
    // Override point for customization after application launch. 

    let navigation = UINavigationController(rootViewController: MainViewController()) 
    self.window?.rootViewController = navigation 
    self.window?.makeKeyAndVisible() 
    return true 
} 

您MainViewController

func buttonTarget() { 

    var vcPass = SecondViewController() 
    vcPass.stringPassed = label.text! 
    self.navigationController?.pushViewController(vcPass, animated: true) 

} 

傳遞字符串

現在選擇在故事板其他控制器,並添加下面的SecondViewController類聲明這個變量權:

class SecondViewController: UIViewController { 


let secondLabel = UILabel() 

    var stringPassed = "" 

使應用程序分配該變量的值與所述viewDidLoad中)下面的行的代碼()方法

FUNC setupLabelSecond({

secondLabel.frame = CGRect(x: 40, y: 80, width: 300, height: 60) 

    if let outputText = stringPassed 
    { 
    secondLabel.text = outputText 
    }else 
    { 
     secondLabel.text = "this is Second Page" 
    } 
    secondLabel.textColor = UIColor.yellow 
    secondLabel.font = UIFont.boldSystemFont(ofSize: 25) 
    secondLabel.textAlignment = .center 
    secondLabel.layer.borderWidth = 2 
    secondLabel.layer.borderColor = UIColor.yellow.cgColor 
    secondLabel.layer.cornerRadius = 5 
    view.addSubview(secondLabel) 
    } 
+0

@NazmulHasan - ru使用Xib否則 –

+0

可能不是!他給我簡單的郵件 –

+0

@NazmulHasan - 赦免,我準備好幫忙,但你如何創建第二個控制器框架 –

1
到secondLabel

我知道2種可能的方式。

1)將您的初始視圖控制器嵌入到導航控制器中。 在你的第二個視圖中創建一個var。例如

var x = ""

在第一視圖中創建一個文本字段出口和第二視圖控制器的對象,

@IBOutlet weak var enteredName: UITextField! 

let secondVC = self.storyboard?.instantiateViewController(withIdentifier: "secondViewController") as! secondViewController 

然後,通過該對象變量x分配所輸入的文本字段。

secondVC.x = enteredName.text! 

在您的第二個vc中,您可以將x的值分配給您的標籤。

mylabel.text = x 

____________________________________________________________________

第二種方法是使用用戶默認值。

接受用戶輸入並將其設置爲用戶默認值。

let defaults = UserDefaults.standard 
defaults.set(self.enteredName.text!,forKey: "userName") 
    defaults.synchronize() 

,並在你的第二個視圖使用

let defaults = UserDefaults.standard 
yourlabel.text = defaults.object(forKey: "userName") 
相關問題