2015-10-05 19 views
0

我的應用程序應該像這樣工作。問題斯威夫特發送從第一個VC數據的第四個

的問題是,沒有出現在lastVC(空標籤)

到底哪裏出問題了?

import UIKit 

class MyChoices { 

var colour : String? 
var style : String? 
var size : String? 



} 


class VC1: UIViewController { 


@IBOutlet weak var nextOutlet: UIButton! 
@IBOutlet weak var colourLabel: UILabel! 

var choice : MyChoices? 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    nextOutlet.hidden = true 


} 

@IBAction func redButton(sender: AnyObject) { 
    nextOutlet.hidden = false 

    colourLabel.text = "Red colour selected" 

    choice?.colour = "Red" 

} 


@IBAction func blueButton(sender: AnyObject) { 

    nextOutlet.hidden = false 

    colourLabel.text = "Blue colour selected" 

    choice?.colour = "Blue" 


} 

@IBAction func greenButton(sender: AnyObject) { 

    nextOutlet.hidden = false 

    colourLabel.text = "Green colour selected" 

    choice?.colour = "Green" 

} 

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 

    if segue.identifier == "lastSegue" { 
    let nextVC = segue.destinationViewController as! lastVC 
    nextVC.choice = self.choice 
    } 

} 




} 

VC2和VC3都非常接近VC1

class lastVC: UIViewController { 


@IBOutlet weak var colourLabel: UILabel! 
@IBOutlet weak var styleLabel: UILabel! 
@IBOutlet weak var sizeLabel: UILabel! 

var choice : MyChoices? 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    colourLabel.text = choice?.colour 
    styleLabel.text = choice?.style 
    sizeLabel.text = choice?.style 



} 
+0

你應該爲你的數據的數據模型/數據類,然後用它來填補標籤。看看這個:http://stackoverflow.com/questions/32518715/persist-data-and-pass-to-multiple-views – Skywalker

+0

創建一個Model類的sharedInstance,並在你的每個viewController中使用它。 –

回答

0

問題是你的對象初始化。您從未初始化您的MyChoice類以獲取您個人風險投資公司內的對象。我只是在遊樂場的代碼下面運行,它的功能非常魅力。所以,在癥結,更換您的var choice : MyChoices?var choice : MyChoices? = MyChoices()

class MyChoices { 
    var colour : String? 
    var style : String? 
    var size : String? 
} 

class VC1: UIViewController { 

    var choice : MyChoices? = MyChoices() 

    func testMe1() { 
     self.choice?.colour = "Red" 

     print("VC1 = \(self.choice?.colour)") 
     let vc2 = VC2() 
     vc2.choice = self.choice 
     vc2.testMe2() 
    } 
} 


class VC2: UIViewController { 

    var choice : MyChoices? = MyChoices() 


    func testMe2() { 
     print("VC2 = \(choice?.colour)") 

     choice?.style = "My Style" 
     let vc3 = VC3() 
     vc3.choice = self.choice 
     vc3.testMe3() 
    } 
} 


class VC3: UIViewController { 

    var choice : MyChoices? = MyChoices() 


    func testMe3() { 
     print("VC3 = \(choice?.style)") 

     choice?.size = "10" 
     let vc4 = VC4() 
     vc4.choice = self.choice 
     vc4.testMe4() 
    } 
} 


class VC4: UIViewController { 

    var choice : MyChoices? = MyChoices() 


    func testMe4() { 
     print("VC4 = \(choice?.size)") 
    } 
} 

// Forceful run to check object properties 
let vc1 = VC1() 
vc1.testMe1()