我正在尝试推送一个ViewController,但我的应用程序每次都崩溃了。
我正在使用UIImagePickerControllerDelegate来拍照,一旦拍完照片,这段代码就会运行:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
let pickedImage = info[UIImagePickerControllerEditedImage] as! UIImage
// Reset the cameraButton and Tabbar
self.cameraButton.setImage(UIImage(named: "camera-button"), forState: .Normal)
self.cameraButton.transform = CGAffineTransformMakeScale(1, 1)
self.tabBarController?.tabBar.alpha = 1
let realmImage = Image()
realmImage.imagedata = UIImageJPEGRepresentation(pickedImage, 1.0)
let birthmark = Birthmark()
//birthmark.imagesArray = RLMArray()
birthmark.imagesArray.addObject(realmImage)
birthmark.bodyPart = Birthmark.Bodypart.LeftArm
realm.beginWriteTransaction()
realm.addObject(birthmark)
realm.commitWriteTransaction()
self.dismissViewControllerAnimated(true, completion: nil)
let secondViewController = self.storyboard!.instantiateViewControllerWithIdentifier("ItemViewController") as! ItemViewController
secondViewController.existingItem = birthmark
navigationController!.presentViewController(secondViewController, animated: true, completion: nil)
}
但是,它总是在最后一行崩溃:
navigationController!.presentViewController(secondViewController, animated: true, completion: nil)
XCode说:EXC_BREAKPOINT(代码= 1,子代码= 0x100489474)
你有什么指示我做错了吗?
这是我的故事板的样子:
堆栈跟踪:
我的猜测是你的navigationController
是nil
。强制它使用!
解包将导致崩溃。确保navigationController
不是nil
。
我的建议尝试推动新视图控制器关闭视图编译,
self.dismissViewControllerAnimated(true, completion: {
let secondViewController = self.storyboard!.instantiateViewControllerWithIdentifier("ItemViewController") as! ItemViewController
secondViewController.existingItem = birthmark
navigationController!.presentViewController(secondViewController, animated: true, completion: nil)
});
我知道问题已经解决但在我的情况下:我从一个控制器故事板复制了一个按钮到另一个,并且按钮插座被复制到新控制器并导致崩溃。