2015-10-30 64 views
0

我正在構建一個非常簡單的使用兩個視圖控制器 - 「ViewControler」和「AnimalChooserViewControler」的swift應用程序。首先有一個簡單的標籤和一個帶有一個項目的工具欄,將用戶轉移到第二個屏幕。第二,有自定義選取器。該應用程序的全部目的是顯示用戶使用輸出UILabel從第二個視圖控制器中選擇的第一個控件。我不得不提到,我是通用xCode 6.4(ios8)發現零,同時展開一個ViewController可選

我的問題是當我嘗試將presentViewController作爲! ViewController該應用程序崩潰與「致命錯誤:意外地發現零,同時解開一個可選值 (lldb)」例外。如何解決這個問題,有什麼建議? 這是我在ViewControler代碼:

class ViewController: UIViewController { 
@IBOutlet weak var outputLabel: UILabel! 

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

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

func displayAnimal(choosenAnimal: String, withSound choosenSound: String, fromComponent choosenComponent: String){ 
    self.outputLabel.text = "You changed \(choosenComponent) (\(choosenAnimal)(and the sound \(choosenSound))" 
} 

} 

這是我在AnimalChooserViewControler

class AnimalChooserViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate{ 

let kComponentCount: Int = 2 
let kAnimalComponent: Int = 0 
let kSoundComponent: Int = 1 

var animalNames: [String] = [] 
var animalSounds: [String] = [] 
var animalImages: [UIImageView] = [] 

@IBAction func dismisAnimalChooser(sender: AnyObject) { 
    dismissViewControllerAnimated(true, completion: nil) 
} 

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { 
    return kComponentCount 
} 

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 
    if component == kAnimalComponent{ 
     return animalNames.count 
    } else { 
     return animalSounds.count 
    } 
} 

func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView { 

    if component == kAnimalComponent { 
     let choosenImageView: UIImageView = animalImages[row] 

     let workarroundImageView: UIImageView = UIImageView(frame: choosenImageView.frame) 
     workarroundImageView.backgroundColor = UIColor(patternImage: choosenImageView.image!) 

     return workarroundImageView 
    } else { 
     let soundLabel: UILabel = UILabel(frame: CGRectMake(0, 0, 100, 32)) 
     soundLabel.text = animalSounds[row] 

     return soundLabel 
    } 
} 

func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat { 
    return 55.0 
} 

func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat { 
    if component == kAnimalComponent{ 
     return 75.0 
    } else { 
     return 150.0 
    } 
} 

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 

    let initialView : ViewController = presentedViewController as! ViewController 

    if component == kAnimalComponent{ 
     let choosenSound: Int = pickerView.selectedRowInComponent(self.kSoundComponent) 
     initialView.displayAnimal(animalNames[row], withSound: animalSounds[choosenSound], fromComponent: "the Animal") 
    } else { 
     let choosenAnimal: Int = pickerView.selectedRowInComponent(kAnimalComponent) 
     initialView.displayAnimal(animalNames[choosenAnimal], withSound: animalSounds[row], fromComponent: "the Sound") 
    } 


} 




override func viewDidLoad() { 
    super.viewDidLoad() 

    animalNames = ["Mouse","Goose","Cat","Dog","Snake","Bear","Pig"] 
    animalSounds = ["Oink","Rawr","Sss","Meow","Honk","Squeak"] 
    animalImages = [UIImageView(image: UIImage(named: "mouse.png")), 
     UIImageView(image: UIImage(named: "goose.png")), 
     UIImageView(image: UIImage(named: "cat.png")), 
     UIImageView(image: UIImage(named: "dog.png")), 
     UIImageView(image: UIImage(named: "snake.png")), 
     UIImageView(image: UIImage(named: "bear.png")), 
     UIImageView(image: UIImage(named: "pig.png"))] 

    preferredContentSize = CGSizeMake(340,380) 

} 

override func viewDidAppear(animated: Bool) { 
    super.viewDidAppear(animated) 

    let initialView: ViewController = presentedViewController as! ViewController 
    initialView.displayAnimal(animalNames[0], withSound: animalSounds[0], fromComponent: "nothing yet...") 


} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 


/* 
// MARK: - Navigation 

// In a storyboard-based application, you will often want to do a little preparation before navigation 
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
// Get the new view controller using segue.destinationViewController. 
// Pass the selected object to the new view controller. 
} 
*/ 

} 
+1

首先從來沒有做過第一次投,第二次使用調試方法,並看看什麼對象,你會得到presentViewController之前,你會施放它。 在你放棄嘗試之前,這可能會回答你的問題。 – Shial

+0

謝謝你的回答。如果let子句我試過解開它,但其他條件總是被觸發(nil)。我只是在調用presentViewController時檢查了我得到的結果,它是零。我在這裏問了這個問題,找出爲什麼它是零 –

回答

0

代碼,我發現是什麼原因導致的除外。我用「presentsViewController」而不是「presentsViewController」。愚蠢的錯誤,但它花了我一個小時才發現它:)

相關問題