NSNotificationCenter。将数据从一个发送到其他ViewControllers Swift

时间:2015-10-09 14:50:22

标签: ios iphone swift

我试图在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相同

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


}

1 个答案:

答案 0 :(得分:0)

当您发布其他ViewControllers的通知时,您确定lastVC是否存在?如果你把它作为正常节目的一部分而变为segue,那么每当你转向它时,视图控制器都会重新创建。

如果要将数据从一个viewController传递到另一个viewController,则应该在prepareForSegue中执行此操作。我会这样接近它

1)创建一个对象:

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

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

3)传递它准备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
    }
  }
}