如标题所述,我想更改位于第二个视图中的标签文本。但它没有工作从第一个按钮点击我需要前进和后退它可以改变有人可以协助我吗?
这是我的第一个初始视图控制器,它包含第二个的旅行按钮
import UIKit
let vc1 = ViewController()
class ViewController2: UIViewController {
@IBAction func QickModeButton(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
let name = Notification.Name(rawValue: quickgameNotificationKey)
NotificationCenter.default.post(name: name , object: nil)
}
@IBAction func chooseButton(_ sender: Any) {
let name = Notification.Name(rawValue: bestof3NotificationKey)
NotificationCenter.default.post(name: name , object: nil)
self.dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
这是标签的第二个
import UIKit
extension UIColor {
static var random: UIColor {
return UIColor(red: .random(in: 0...1),
green: .random(in: 0...1),
blue: .random(in: 0...1),
alpha: 1.0)
}
}
let bestof3NotificationKey = "co.Drake.bestof3"
let quickgameNotificationKey = "co.Drake.quick"
class ViewController: UIViewController {
let bestof3 = Notification.Name(rawValue: bestof3NotificationKey)
let quick = Notification.Name(rawValue: quickgameNotificationKey)
deinit {
NotificationCenter.default.removeObserver(self)
}
@IBOutlet var CommunicationLabel: UILabel!
@IBOutlet var playAgainoutlet: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
self.createobservers()
winningLabel.isHidden = true
winningLabel.center = CGPoint(x: winningLabel.center.x, y: winningLabel.center.y - 400)
playAgainoutlet.isHidden = true
playAgainoutlet.center = CGPoint(x: playAgainoutlet.center.x, y: playAgainoutlet.center.y + 400)
}
func createobservers(){
// quick mode button observer
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.updateLabel(notification:)), name: quick, object: nil)
// bestof3 mode button observer
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.updateLabel(notification:)), name: bestof3, object: nil)
}
@objc func updateLabel(notification:NSNotification){
let isbestof3 = notification.name == self.bestof3 // this here is setting up a variable if notification.name == bestof3 (isbestof3 will then be equal to true)
let labeltext = isbestof3 ? "Best of 3" : "quick"
self.CommunicationLabel.text = labeltext
}
}
感谢任何帮助,谢谢!
第二个ViewController
的viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
self.createobservers()
没有被调用,因为它尚未呈现。在显示视图之前没有观察者。 (loaded
)