簡體   English   中英

將Segue圖像發布給另一個ViewController

[英]Issue with Segue image to another viewcontroller

我有2 VC:

點擊第一個帶有1個按鈕的 VC >> 2個選項可發布圖像表格cam

第二VC 選擇了與第二VC相連的圖像形式VC1

我試圖弄清楚這個錯誤沒有成功

致命錯誤:解開指向的Optional值時意外發現nil

  imageView.image=info[UIImagePickerControllerOriginalImage] as? UIImage

我將項目上傳到了保管箱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
    }
}

您的情節提要接線錯誤。

該圖像實際上應該設置在second實例上,而不是在ViewController.實例上ViewController. 請注意,崩潰發生在ViewController而不是second

這段代碼:

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

試圖設置imageView.imageViewController 如果查看情節提要,則ViewController沒有UIImageView

在此處輸入圖片說明

ViewController上定義imageView的方式是導致此崩潰的原因:

@IBOutlet weak var imageView: UIImageView!

! 在這里,您說的是:“這永遠不會為零。” 好吧,顯然不是這樣,因為既然此IBOutlet沒有連接到UIImageView則它為nil。 因此,當您嘗試訪問它時,BOOM!

要解決此問題:

  • 用戶選擇圖像時second
  • secondimage.imageView屬性上設置所選圖像

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM