我有两个Controller,名称分别为FirstViewController
和SecondViewController
FirstViewController
是主页,我在这里添加了UITextView
。
var secondViewController: SecondViewController!
let textView: UITextView = {
let textView = UITextView()
//textView.font = UIFont.systemFont(ofSize: 20)
textView.isEditable = true
textView.keyboardDismissMode = .interactive
textView.allowsEditingTextAttributes = true
return textView
}()
override func viewDidLoad() {
super.viewDidLoad()
secondViewController = SecondViewController()
setUpLayout() //do something like add the textView to the view
addKeyboardObserver() //a function which observe the keyboard
}
然后我添加一个按钮以将secondView显示为键盘
@objc func showSecondView() {
textView.resignFirstResponder()
textView.inputView = secondViewController.view
textView.becomeFirstResoonder()
}
然后我定义一个函数来关闭secondView
func closeSecondView() {
textView.resignFirstResponder()
textView.inputView = nil
}
在我的SecondViewController
var firstViewController: FirstViewController!
override func viewDidLoad() {
super.viewDidLoad()
firstViewController = FirstViewController()
self.view.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 300)
self.view.backgroundColor = .cyan
}
然后我添加一个按钮以关闭firstViewController的自定义键盘
@objc func closeFontView() {
print(123)
firstViewController.closeSecondView()
}
结果是打印“ 123”,然后在closeFontView()
处添加一个断点,它被称为。但是没有显示UI的更改。(未关闭键盘)为什么?我该如何解决?
答案 0 :(得分:1)
问题出在这里[删除此行]
firstViewController = FirstViewController() // new instance not the real presented first
当您显示第二个(firstViewController
里面的东西时,这样连接它
let second = ///
second.first = self
// present second
答案 1 :(得分:0)
在您的MyClass
中,存储secondViewController
的实例。
firstViewController
现在删除var secondViewController: SecondViewController!
let textView: UITextView = {
let textView = UITextView()
//textView.font = UIFont.systemFont(ofSize: 20)
textView.isEditable = true
textView.keyboardDismissMode = .interactive
textView.allowsEditingTextAttributes = true
return textView
}()
override func viewDidLoad() {
super.viewDidLoad()
secondViewController = SecondViewController()
secondViewController.firstViewController = self // <- add this line
setUpLayout() //do something like add the textView to the view
addKeyboardObserver() //a function which observe the keyboard
}
中FirstViewController
的初始化,因为您需要相同实例,而不需要另一个实例。
SecondViewController