gpt4 book ai didi

ios - NSNotificationCenter。将数据从一个发送到另一个 ViewControllers Swift

转载 作者:行者123 更新时间:2023-11-28 08:55:59 24 4
gpt4 key购买 nike

我试图根据用户在每个 View 中的选择在 LastVC 中显示一些数据。我让你看我们App的形象

Image

我正在使用 NSNotificationCenter 但 lastVC 中的标签没有改变。

怎么了?

@IBOutlet weak var colorLabelSelected: UILabel!

@IBOutlet weak var nextOutlet: UIButton!

override func viewDidLoad() {
super.viewDidLoad()

nextOutlet.hidden = true



}


@IBAction func greenButton(sender: AnyObject) {

NSNotificationCenter.defaultCenter().postNotificationName("setGreenColorID", object: nil)

colorLabelSelected.text = "You have selected a green BG color"
nextOutlet.hidden = false


}


@IBAction func blueButton(sender: AnyObject) {

NSNotificationCenter.defaultCenter().postNotificationName("setGreenColorID", object: nil)

colorLabelSelected.text = "You have selected a blue BG color"
nextOutlet.hidden = false

}


@IBAction func pinkButton(sender: AnyObject) {

NSNotificationCenter.defaultCenter().postNotificationName("setGreenColorID", object: nil)

colorLabelSelected.text = "You have selected a pink BG color"
nextOutlet.hidden = false

}

类 ViewController2:UIViewController {

@IBOutlet weak var nextOutlet: UIButton!


@IBOutlet weak var titleLabelSelected: UILabel!


override func viewDidLoad() {
super.viewDidLoad()

nextOutlet.hidden = true

}

@IBAction func newsButton(sender: AnyObject) {

NSNotificationCenter.defaultCenter().postNotificationName("setNewsThemeID", object: nil)

titleLabelSelected.text = "You have selected News as title"
nextOutlet.hidden = false

}


@IBAction func sportButton(sender: AnyObject) {

NSNotificationCenter.defaultCenter().postNotificationName("setSportThemeID", object: nil)

titleLabelSelected.text = "You have selected Sport as title"
nextOutlet.hidden = false

}



@IBAction func wealthButton(sender: AnyObject) {

NSNotificationCenter.defaultCenter().postNotificationName("setWealthThemeID", object: nil)

titleLabelSelected.text = "You have selected Wealth as title"
nextOutlet.hidden = false

}

与VC3相同

类 lastViewController: UIViewController {

@IBOutlet weak var finalColorLabel: UILabel!


@IBOutlet weak var finalThemeLabel: UILabel!


@IBOutlet weak var finalRelevanceLabel: UILabel!





override func viewDidLoad() {
super.viewDidLoad()

NSNotificationCenter.defaultCenter().addObserver(self, selector: "setBlueColor:", name: "setBlueColorID", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "setGreenColor:", name: "setGreenColorID", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "setPinkColor:", name: "setPinkColorID", object: nil)


NSNotificationCenter.defaultCenter().addObserver(self, selector: "setNewsTheme:", name: "setNewsThemeID", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "setSportTheme:", name: "setSportThemeID", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "setWealthTheme:", name: "setWealthThemeID", object: nil)

NSNotificationCenter.defaultCenter().addObserver(self, selector: "setTenPoints:", name: "setTenPointsID", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "setSevenPoints:", name: "setSevenPointsID", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "setFivePoints:", name: "setFivePointsID", object: nil)




}


//setColors
func setBlueColor(notification: NSNotification) {

finalColorLabel.text = "blue"


}


func setGreenColor(notification: NSNotification) {

finalColorLabel.text = "green"


}

func setPinkColor(notification: NSNotification) {

finalColorLabel.text = "pink"


}

//setTheme

func setNewsTheme(notification: NSNotification) {

finalThemeLabel.text = "news"


}


func setSportTheme(notification: NSNotification) {

finalThemeLabel.text = "sport"


}


func setWealthTheme(notification: NSNotification) {


finalThemeLabel.text = "wealth"


}



//setRelevance

func setTenPoints(notification: NSNotification){

finalRelevanceLabel.text = "ten"


}


func setSevenPoints(notification: NSNotification){

finalRelevanceLabel.text = "seven"


}

func setFivePoints(notification: NSNotification){

finalRelevanceLabel.text = "five"


}

最佳答案

当您发布来自其他 ViewController 的通知时,您确定 lastVC 是否存在?如果您将其作为普通 show 的一部分继续访问,则每次您继续访问时都会重新创建 View Controller 。

如果您想将数据从一个 viewController 传递到另一个 viewController,您应该在 prepareForSegue 中进行。我会这样处理

1) 创建一个对象:

struct userSettings {
var backgroundColor: NSColor?
var title: String?
var relevance: Int?
}

2) 在每个 ViewController 中填写适当的字段

3) 将它传递给 prepare for segue

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let identifier = segue.identifier {
switch identifier {
case segueToController:
if let viewController = segue.destinationViewController as? myControllerClass {
viewController.userSettings = userSettings
}
default: break
}
}
}

关于ios - NSNotificationCenter。将数据从一个发送到另一个 ViewControllers Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33041275/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com