将Segue图像问题发送到另一个viewcontroller

时间:2015-11-02 19:07:48

标签: ios iphone uiviewcontroller uiimageview segue

我是2 VC:

点按时第一个VC 带1个按钮>> 2个选项张贴图像形式凸轮图书馆

第二个VC 选择的图像表格VC1被伪装到第二个VC

我试图想出这个错误没有任何成功

  

致命错误:在展开Optional值时意外发现nil   指着

  imageView.image=info[UIImagePickerControllerOriginalImage] as? UIImage

我在dropbox https://www.dropbox.com/s/w7blbnyac9qyxgw/segImage.zip?dl=0

中上传了该项目

VC1

    class ViewController:UIViewController,UIAlertViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIPopoverControllerDelegate
{
    @IBOutlet weak var btnClickMe: UIButton!
    @IBOutlet weak var imageView: UIImageView!
    var picker:UIImagePickerController?=UIImagePickerController()
    var popover:UIPopoverController?=nil

    override func viewDidLoad()
    {
        super.viewDidLoad()
        picker!.delegate=self
        // 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.
    }



    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if (segue.identifier == "" ) {
            let destVC : second = segue.destinationViewController as! second
            destVC.pics = imageView.image!
        }
    }

    @IBAction func btnImagePickerClicked(sender: AnyObject)
    {
        let alert:UIAlertController=UIAlertController(title: "Choose Image", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)

        let cameraAction = UIAlertAction(title: "Camera", style: UIAlertActionStyle.Default)
            {
                UIAlertAction in
                self.openCamera()

        }
        let gallaryAction = UIAlertAction(title: "Gallary", style: UIAlertActionStyle.Default)
            {
                UIAlertAction in
                self.openGallary()
        }
        let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel)
            {
                UIAlertAction in

        }

        // Add the actions
        picker?.delegate = self
        alert.addAction(cameraAction)
        alert.addAction(gallaryAction)
        alert.addAction(cancelAction)
        // Present the controller
        if UIDevice.currentDevice().userInterfaceIdiom == .Phone
        {
            self.presentViewController(alert, animated: true, completion: nil)
        }
        else
        {
            popover=UIPopoverController(contentViewController: alert)
            popover!.presentPopoverFromRect(btnClickMe.frame, inView: self.view, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)
        }
    }
    func openCamera()
    {
        if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera))
        {
            picker!.sourceType = UIImagePickerControllerSourceType.Camera
            self .presentViewController(picker!, animated: true, completion: nil)
        }
        else
        {
            openGallary()
        }
    }
    func openGallary()
    {
        picker!.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        if UIDevice.currentDevice().userInterfaceIdiom == .Phone
        {
            self.presentViewController(picker!, animated: true, completion: nil)
        }
        else
        {
            popover=UIPopoverController(contentViewController: picker!)
            popover!.presentPopoverFromRect(btnClickMe.frame, inView: self.view, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)
        }
    }
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
    {
        picker .dismissViewControllerAnimated(true, completion: nil)
        imageView.image=info[UIImagePickerControllerOriginalImage] as? UIImage
    }
    func imagePickerControllerDidCancel(picker: UIImagePickerController)
    {
        print("picker cancel.")
    }


}

第二个VC

class second: UIViewController {

var pics = UIImage()

@IBOutlet var image: UIImageView!
override func viewDidLoad() {
    super.viewDidLoad()

    if let img = UIImage(named: "pics")
    {
        self.image.image = img
    }
}

1 个答案:

答案 0 :(得分:1)

您的故事板布线错误。

应该在second的实例而不是ViewController.的实例上设置图片。请注意,崩溃发生在ViewController而不是second

此代码:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
    picker .dismissViewControllerAnimated(true, completion: nil)
    imageView.image=info[UIImagePickerControllerOriginalImage] as? UIImage
}

正在尝试在imageView.image中设置ViewController。如果您查看故事板,则UIImageView中没有ViewController

enter image description here

您在imageView上定义ViewController的方式是导致此次崩溃的原因:

@IBOutlet weak var imageView: UIImageView!

在那里!,你说,"这永远不会是零。"嗯,显然不是这种情况,因为因为这个IBOutlet没有连接到UIImageView,所以它是零。因此,当你试图访问它时,BOOM!

解决此问题:

  • 当用户选择图片时,second选择
  • second' s image.imageView属性
  • 上设置所选图片

其他建议:   - 在命名中使用一致性,例如second vs SecondViewController