2016-11-04 24 views
0

我試圖在通過HDMI連接設備時在ExternalViewController中的MainViewController和UITextField上顯示一個按鈕。當在MainViewController中發生點擊時,我需要更新ExternalViewController中的UITextField。我可以看到打印出現在輸出窗口中,但文本字段不更新。在主視圖控制器中單擊後更新外部視圖控制器中的UITextField

MainViewController.swift

import UIKit 
import WebKit 

class MainViewController: UIViewController { 
    fileprivate var externalWindow: UIWindow? 

    override func viewDidLoad() { 
    super.viewDidLoad() 

    // Do any additional setup after loading the view. 
    if UIScreen.screens.count > 1 { 
     setupExternalScreen(UIScreen.screens[1]) 
    } 

    let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50)) 
    button.backgroundColor = UIColor.blue 

    button.setTitle("Click Me", for: UIControlState.normal) 
    button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside) 

    self.view.addSubview(button) 

    } 

    override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
    } 


    /* 
    // MARK: - Navigation 

    // In a storyboard-based application, you will often want to do a little preparation before navigation 
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    // Get the new view controller using segue.destinationViewController. 
    // Pass the selected object to the new view controller. 
    } 
    */ 
    fileprivate func setupExternalScreen(_ screen: UIScreen) { 
    guard externalWindow == nil, 
     let vc = self.storyboard?.instantiateViewController(withIdentifier: "ExternalScreen") as? ExternalViewController else { 
     return 
    } 

    externalWindow = UIWindow(frame: screen.bounds) 
    externalWindow!.rootViewController = vc 
    externalWindow!.screen = screen 
    externalWindow!.isHidden = false 
    } 

    func buttonAction(sender: UIButton) { 
    print("Button tapped") 
    ExternalViewController().updateLabel() 
    } 

} 

ExternalViewController.swift

import UIKit 

class ExternalViewController: UIViewController { 

    let output = UITextField(frame: CGRect(origin: CGPoint(x: 0,y :0), size: CGSize(width: 300, height: 100))) 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.addTextField() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func addTextField() { 
     output.textColor = UIColor.black 
     output.text = "This is the other text field" 
     view.addSubview(output) 
    } 

    func updateLabel() { 
     print("inside updateLabel") 
     output.text = "button was clicked" 
    } 

} 

這是怎麼樣子。

enter image description here

這是我與斯威夫特的第一個項目,所以我道歉,如果這是一個壞的問題。

+1

你需要數據傳遞字符串或使用委託。 – KKRocks

+0

檢查我的答案。如果你在iOS上,你應該考慮展示ExternalViewController,而不是在一個新的窗口中顯示它,至少有一種奇怪的方法 – DatForis

回答

1

嘗試使用NotificationCentre。 在ExternalVC

override func viewDidLoad() { 
    super.viewDidLoad() 
    NotificationCenter.default.addObserver(self, selector: #selector(receivedDataFromNotification(notification:)), name: NSNotification.Name(rawValue: "passdata"), object: nil) 
    } 

    func receivedDataFromNotification(notification : NSNotification) -> Void { 
     print(notification.object); 
     output.text = "button was clicked" 
    } 

在MainViewController

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "passdata"), object: "your string pass here") 
0

雖然你可以使用通知之間我更喜歡創建委託來傳輸數據來傳輸數據。 首先創建一個協議

protocol ExternalViewControllerDelegate: class{ 
    func shouldUpdateLabel(withText text: String) 
} 

然後更新ExternalViewController適當地以包含代表這當然

class ExternalViewController: UIViewController { 
    weak var delegate: ExternalViewControllerDelegate? 
    let output = UITextField(frame: CGRect(origin: CGPoint(x: 0,y :0), size: CGSize(width: 300, height: 100))) 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.addTextField() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func addTextField() { 
     output.textColor = UIColor.black 
     output.text = "This is the other text field" 
     view.addSubview(output) 
    } 

    func updateLabel() { 
     print("inside updateLabel") 
     output.text = "button was clicked" 
     delegate?.shouldUpdateLabel(withText: "Your text") 
    } 
} 

的弱引用記得撥打在委託方法。我在類中使用updateLabel方法來調用方法。我想你也想使用

最後在MainViewController中實現協議並記得設置委託。

extension MainViewController: ExternalViewControllerDelegate{ 
    func shouldUpdateLabel(withText text: String) { 
     //Do what you want with the text 
    } 
} 

然後更新setupExternalScreen方法來設置委託

func setupExternalScreen(_ screen: UIScreen) { 
    guard externalWindow == nil, 
     let vc = self.storyboard?.instantiateViewController(withIdentifier: "ExternalScreen") as? ExternalViewController else { 
      return 
    } 
    vc.delegate = self 

    externalWindow = UIWindow(frame: screen.bounds) 
    externalWindow!.rootViewController = vc 
    externalWindow!.screen = screen 
    externalWindow!.isHidden = false 
} 
相關問題