我在iPhone5s iOS7上运行了我的应用程序。按钮的索引是错误的,cancleButton成为第一个按钮。我很困惑,当我点击背景时,它会打开相册。我不知道为什么。太奇怪了。
这是我的代码。
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)
}
答案 0 :(得分:0)
添加
时let actionSheet = UIActionSheet(title: nil, delegate: self, cancelButtonTitle: "cancle", destructiveButtonTitle: nil)
这一行它会将取消按钮添加到索引0,然后在它之后添加按钮
actionSheet.addButtonWithTitle("album")
if UIImagePickerController.isSourceTypeAvailable( UIImagePickerControllerSourceType.Camera) {
actionSheet.addButtonWithTitle("camera")
}
索引1&的专辑按钮索引2处的相机
所以如果你想要最后一个索引的取消按钮,那么你需要添加像
actionSheet.addButtonWithTitle("Cancel")
添加“相册和相机”按钮后。
答案 1 :(得分:0)
试试这个
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);
如果您按上述方式添加取消按钮,则取消按钮将显示在AlertController的末尾