2016-12-03 52 views
0

我遇到了一個問題,我似乎無法理解它爲何發生。通過Prepare for segue傳輸數據

Ive created a class called "Apples" on a new Swife and called it Apples.

class Apples { 
    var points = points() 
    var appleName = String? 
} 

constructed a struct called points to define the apple parameters

struct points { 

    var taste = 0 
    var smell = 0 
    var texture = 0 
    var color = 0 


} 

and constructed a function to make new apples

func newApples(t:Int, s: Int, tex: Int, col: Int, appName = String) 
{ 
    let newApple = Apples() 
    newApple.points.taste = t 
    newApple.points.smell = s 
    newApple.points.texture = tex 
    newApple.points.color = col 
newApple.appleName = appName 
} 

and then I got 2 ViewControllers, 1 is AppleViewController, and the 2nd is ChoiceViewController.

I called

let pinkLady = newApples(t: 5, s: 4, tex: 4, col: 5, appName = "Pink Lady") 

on the AppleViewController

On the ChoiceViewController I got some UILabel Outlet(appleLabel) which I want to change according to the Apple the user picked on the 1st AppleViewController

the problem lies when I try to make a Prepare(for segue:) function

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     let choiceVC = segue.destination as! ChoiceViewController 
     choiceVC.appleLabel.text = pinkLady.appleName 
    } 

And in that last line of code lies the problem, When im running the app and click the button with no line of code in (prepare for segue) function it performs the segue just fine, but with it it calls "fatal error: unexpectedly found nil while unwrapping an Optional value"

I tried all sort of syntax such as

choiceVC.appleLabel.text = ((pinkLady.appleName)!) 
choiceVC.appleLabel.text = pinkLady.appleName? 
choiceVC.appleLabel.text = ("\(pinkLady.appleName?)") 

and then I tried to just put

print((pinkLady.appleName)!)

inside the prepare(for segue) function and when it performed it printed out to the console Pink Lady with no problem. i really dont know what im missing here.

+0

你的問題是'致命錯誤:意外地發現零,而解開一個可選值'這意味着一個零它不應該是。在prepareForSegue中設置一個斷點並逐行進行,直到找到零。 – Multinerd

+0

我認爲這個問題是因爲你的appleLabel沒有在內存中分配而引起的。嘗試declate在choiseVC一個變種srting,然後在viewDidLoad中FUNC,將其分配給UILabel的文本 – Asike

+0

正如我所說的,它的通話零上線到appleLabel.text改變 pinkLady.appleName 但是當IM打印相同的值它不返回零,它返回粉紅色夫人 –

回答

0

我覺得出現這個問題,因爲你的appleLabel未在內存中分配呢。嘗試在choiseVC中聲明var字符串,然後在viewdidload func中將其分配給uilabel的文本。

0

首先你是通過輸入靜態值來嘗試它。

choiceVC.appleLabel.text = "This Is Apple" 

使用這個靜態數據,你沒有得到任何錯誤,這意味着你的代碼是正確的。問題是pinkLady.appleName found nil。

其他明智地使用這個代碼,而不是賽格瑞代碼

let choiceVC = self.storyboard?.instantiateViewController(withIdentifier: "ChoiceViewController") as? ChoiceViewController 
choiceVC.appleLabel.text = (pinkLady.appleName 
self.navigationController?.pushViewController(choiceVC!, animated: true) 
-1
Make an object say pinkLadyInChoice, in ChoiceVC of type Apples. 
and pass pinkLady object to declared above, here pinkLadyInChoice, through prepareForSegue Method. 
Then in ChoiceVC set text of labels by using object pinkLadyInChoice here. 

從ApplesVC傳遞pinkLady目的是ChoiceVC strPinkLady對象

在choiceVC編寫如下: -

let strPinkLady : Apples 
choiceVC.appleLabel.text = strPinkLady.appleName 
0

試試這個代碼...

let strPinkLady : String = pinkLady.appleName? 
choiceVC.appleLabel.text = strPinkLady