這可以使用NotificationCenter
完成。在您的ViewController2加觀察者(斯威夫特3版):
func viewDidLoad() {
NotificationCenter.default.addObserver(self, selector: #selector(arrayUpdateNotificationReceived :), name: "TextFieldInputChangedNotification", object: nil)
}
func arrayUpdateNotificationReceived (notification : Notification) {
yourArray.append(notification.object);
}
在ViewControlller1:
當用戶點擊添加上彈出:
@IBAction func yourButtonAction(sender : UIButton) {
let inputText = inputTextField.text;
notificationCenter.post(name: "TextFieldInputChangedNotification", object: inputText)
}
不要忘了在你的ViewController 2刪除觀察員:
deinit {
NotificationCenter.default.removeObserver(self)
}
我敢肯定,這是正確的,但我試圖迅速做到這一點,如果我沒有錯誤的在目標C?不管怎樣,謝謝你! – Alvarsson
這裏是一個迅速的3版本:) –
感謝您將此翻譯爲swift,好像使用swift 4和NotificationCenter中的某處代碼被depricated並且不起作用。但無論如何謝謝你! :) – Alvarsson