2016-12-03 51 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