2016-11-04 14 views
0

デバイスがHDMI経由で接続されている場合、MainViewControllerのボタンとExternalViewControllerの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

を設定するsetupExternalScreen方法を更新します。 – KKRocks

+0

私の答えをチェックしてください。あなたがiOSを利用している場合、ExternalViewControllerを新しいウィンドウに表示せずに提示することを考えてください – DatForis

答えて

1

NotificationCentreを使用してみてください。 あなたは、私がデータを転送するためのデリゲートを作成好む間でデータを転送するために、通知を使用することができますがMainViewController

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

で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" 
    } 

で。 は、最初のコース

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") 
    } 
} 

の弱参照は、デリゲートにメソッドを呼び出すことを忘れないデリゲートを含有する適切ExternalViewControllerを更新し、プロトコル

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

を作成します。メソッドの呼び出しには、クラスのupdateLabelメソッドを使用しました。あなたも使用したいと思うもの

最後に、MainViewControllerでプロトコルを実装し、デリゲートを設定することを忘れないでください。

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

その後、あなたは文字列またはデリゲートを使用してデータを渡す必要があるデリゲート

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 
} 
関連する問題