ios - 在主视图 Controller 中单击后更新外部 View Controller 中的 UITextField

标签 ios swift uitextfield swift3

当设备通过 HDMI 连接时,我试图在 MainViewController 上显示一个按钮,在 ExternalViewController 中显示一个 UITextField。当在 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()
  }

}

外部 View Controller .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 的第一个项目,所以如果这是一个糟糕的问题,我深表歉意。

最佳答案

尝试使用 NotificationCentre 。 在外部VC中

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

在主视图 Controller 中

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

关于ios - 在主视图 Controller 中单击后更新外部 View Controller 中的 UITextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40416295/

相关文章:

swift - 稍后在 Xcode 中生成 .strings 文件

ios - 如何从 TableView 中自定义单元格中的标签获取文本

iphone - 如何找出 UITextField 中的当前插入符位置?

ios - xcode 将不同的最大字符长度设置为不同的 TextField

iphone - unsignedIntValue 与 intValue

iphone - 提供 iOS 访问在线文件

ios - 使用文本字段聚焦错误 View

Swift & Vapor/Fluent : How to pull values from another table during a post router request

ios - 滚动后 UITableViewCell 标签发生变化

ios - 在横向模式下切换 View 时 Xcode 通用应用程序问题