Segue 图像到另一个视图控制器的问题
Posted
技术标签:
【中文标题】Segue 图像到另一个视图控制器的问题【英文标题】:Issue with Segue image to another viewcontroller 【发布时间】:2015-11-02 19:07:47 【问题描述】:我有 2 个 VC:
第一个 VC 点击时带有 1 个按钮 >> 2 个选项 发布图像表单 cam 或 库
第二个 VC 选择的图像从 VC1 连接到第二个 VC
我试图想通这个错误没有任何成功
致命错误:在展开可选值时意外发现 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】:您的故事板接线错误。
图像实际上应该设置在second
的实例而不是ViewController.
的实例上注意,崩溃发生在ViewController
而不是second
这段代码:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
picker .dismissViewControllerAnimated(true, completion: nil)
imageView.image=info[UIImagePickerControllerOriginalImage] as? UIImage
正在尝试在ViewController
中设置imageView.image
。如果您查看情节提要,ViewController
中没有 UIImageView
。
您在ViewController
上定义imageView
的方式是导致此崩溃的原因:
@IBOutlet weak var imageView: UIImageView!
有了!
,你在说,“这永远不会是零。”好吧,显然不是这样,因为因为这个 IBOutlet 没有连接到 UIImageView
,所以它是 nil。因此,当您尝试访问它时,BOOM!
解决这个问题:
当用户选择图片时转到second
在second
的image.imageView
属性上设置选中的图片
其他建议: - 在命名中使用一致性,例如 second vs SecondViewController
【讨论】:
以上是关于Segue 图像到另一个视图控制器的问题的主要内容,如果未能解决你的问题,请参考以下文章
segue 将数据从 tableviewcontroller 解析到另一个