vendredi 10 avril 2015

iOS 8/Xcode 6 - Add action to AlertSheet button

I am writing a basic photo app that allows a user to push a "+" button in a navigation controller and then an ActionSheet pops up from the bottom giving the user options, here is a picture:


Screenshot of my app


I have the code to get this app working using normal buttons, but when I added it to my code for this app it crashes. Here is the code:



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)
}

}


and the strange thing is after I ran this and the app crashed, I reverted back to when the code was working and the app still crashed.


Aucun commentaire:

Enregistrer un commentaire