0

I ran my app on iPhone5s iOS7. The indexes of buttons are wrong, cancleButton becomes the first button. I am confused that when I tap the background, it opens the album. I don't know why. So strange.

图片在这里

This is my code.

func actionsheetInIOS8Early() {

    let actionSheet = UIActionSheet(title: nil, delegate: self, cancelButtonTitle: "cancle", destructiveButtonTitle: nil)

    actionSheet.addButtonWithTitle("album")

    if UIImagePickerController.isSourceTypeAvailable( UIImagePickerControllerSourceType.Camera) {

        actionSheet.addButtonWithTitle("camera")

    }

    actionSheet.showInView(self.view)

}
Shoaib
  • 2,286
  • 1
  • 19
  • 27
rose
  • 241
  • 5
  • 16
  • That's because in the Buttons array of action sheet, the first element is Cancel button. Other buttons are added after it. So when it loads, cancel button is on the top as it is on index 0. – NSNoob Nov 05 '15 at 06:32

2 Answers2

0

When you add

let actionSheet = UIActionSheet(title: nil, delegate: self, cancelButtonTitle: "cancle", destructiveButtonTitle: nil)

this line it will add Cancel button to index 0 and then after it will add your buttons

actionSheet.addButtonWithTitle("album")

    if UIImagePickerController.isSourceTypeAvailable( UIImagePickerControllerSourceType.Camera) {

        actionSheet.addButtonWithTitle("camera")

    }

Album button at index 1 & Camera at index 2

So If you want Cancel button at last index then you need to add like

    actionSheet.addButtonWithTitle("Cancel")

after adding your button for Album and Camera.

mrunal thanki
  • 753
  • 6
  • 16
  • If so, cancelButton indeed is the third button, but three of them close together, cancleButton should be separate from the first two button and cancleButton is at bottom – rose Nov 05 '15 at 05:44
0

Try this

let optionMenu = UIAlertController(title: "Title", message: "message", preferredStyle: .ActionSheet);

if UIImagePickerController.isSourceTypeAvailable( UIImagePickerControllerSourceType.Camera){
    let cameraAction = UIAlertAction(title: "Camera", style: .Default, handler: {
            (alert: UIAlertAction) -> Void in
            // do something 
        })
    optionMenu.addAction(cameraAction);
}

let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: {
        (alert: UIAlertAction) -> Void in
        print("Cancelled")
    })
optionMenu.addAction(cancelAction);

self.presentViewController(optionMenu, animated: true, completion: nil);

If u add cancel button as above, cancel button will shown at the end of the AlertController

rnsjtngus
  • 225
  • 1
  • 8