在主视图控制器中单击后,在外部视图控制器中更新UITextField

时间:2016-11-04 06:05:36

标签: ios swift uitextfield swift3

我正在尝试在MainViewController上显示一个按钮,并在ExternalViewController中显示UITextField,以便在通过HDMI连接设备时。当MainViewController中发生单​​击时,我需要更新ExternalViewController中的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

这是我与Swift的第一个项目,所以如果这是一个糟糕的问题我会道歉。

2 个答案:

答案 0 :(得分: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")

答案 1 :(得分: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
}