iOS 8/Xcode 6-将操作添加到AlertSheet按钮
我正在编写一个基本的照片应用程序,允许用户按下导航控制器中的“+”按钮,然后从底部弹出一个操作表,为用户提供选项,下面是一张图片: 我有使用普通按钮使此应用程序工作的代码,但当我将其添加到此应用程序的代码中时,它崩溃了。代码如下:iOS 8/Xcode 6-将操作添加到AlertSheet按钮,ios,image,swift,uialertsheet,Ios,Image,Swift,Uialertsheet,我正在编写一个基本的照片应用程序,允许用户按下导航控制器中的“+”按钮,然后从底部弹出一个操作表,为用户提供选项,下面是一张图片: 我有使用普通按钮使此应用程序工作的代码,但当我将其添加到此应用程序的代码中时,它崩溃了。代码如下: import UIKit class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { @IBOutle
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate
{
@IBOutlet weak var imageView: UIImageView!
@IBAction func addPhoto(sender: UIBarButtonItem)
{
let photoOption = UIAlertController(title: nil, message: "Select an Input Type", preferredStyle: .ActionSheet)
let photoLibraryAction = UIAlertAction(title: "Photo Library", style: .Default) { (alert: UIAlertAction!) -> Void in
println("Photo Library Selected") // Used for debugging
// This code worked in my previous app
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .PhotoLibrary
self.presentViewController(picker, animated: true, completion: nil)
}
let cameraAction = UIAlertAction(title: "Camera", style: .Default) { (alert: UIAlertAction!) -> Void in
println("Camera Selected") // Used for debugging
// This code worked in my previous app
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .Camera
self.presentViewController(picker, animated: true, completion: nil)
}
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
photoOption.addAction(photoLibraryAction)
photoOption.addAction(cameraAction)
photoOption.addAction(cancelAction)
self.presentViewController(photoOption, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info: [NSObject: AnyObject]!)
{
imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
dismissViewControllerAnimated(true, completion: nil)
}
}
奇怪的是,在我运行这个程序后,应用程序崩溃了,我恢复到代码运行时,应用程序仍然崩溃。问题是你正在模拟器中运行这个代码。但是模拟器没有摄像头。它只是一个模拟器
因此,尝试调用带有
.Camera
源类型的图像拾取控制器会导致异常-控制台中有一条有用的错误消息,您可以读取该消息,从而避免因这个问题浪费带宽…什么是崩溃?“你不能指望人们在这里猜测或者为你工作。”莱昂纳坦显然是可以期待的。只是他想错了不幸的是,我不再有错误。由于那个应用程序不再工作,我不得不重新开始,我不想故意再次破坏我的代码。当我运行应用程序时,构建成功,模拟器打开,然后立即崩溃。然后我找到了app委托,其中类AppDelegate:UIResponder,uiapplicationelegate
以绿色突出显示,并显示一些消息。我知道这没有多大帮助,但对某些人来说可能听起来很熟悉。不过,模拟器使用的是.PhotoLibrary选项,这正是我所使用的。它在代码中写着picker.sourceType=.Camera
。我只是在看上面写的。