Ответ 1
Вы никогда не устанавливаете делегат листа действия:
myActionSheet = UIActionSheet()
myActionSheet.delegate = self
Я создал лист действий, но проблема в том, что метод делегата не называется
myActionSheet = UIActionSheet()
myActionSheet.addButtonWithTitle("Add event")
myActionSheet.addButtonWithTitle("close")
myActionSheet.cancelButtonIndex = 1
myActionSheet.showInView(self.view)
///UIActionSheetDelegate
func actionSheet(myActionSheet: UIActionSheet!, clickedButtonAtIndex buttonIndex: Int){
if(myActionSheet.tag == 1){
if (buttonIndex == 0){
println("the index is 0")
}
}
}
Я использовал другой способ, который хорошо работал с iOS 8, но не работал с iOS 7:
var ActionSheet = UIAlertController(title: "Add View", message: "", preferredStyle: UIAlertControllerStyle.ActionSheet)
ActionSheet.addAction(UIAlertAction(title: "Add event", style: UIAlertActionStyle.Default, handler:nil))
self.presentViewController(ActionSheet, animated: true, completion: nil)
Любая идея решить проблему?
Вы никогда не устанавливаете делегат листа действия:
myActionSheet = UIActionSheet()
myActionSheet.delegate = self
UIActionSheet в быстром языке: -
Лист действия с cancelButton и destructiveButton
установите UIActionSheetDelegate.
let actionSheet = UIActionSheet(title: "ActionSheet", delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: "Done")
actionSheet.showInView(self.view)
Лист действия с cancelButton, destructiveButton и otherButton
let actionSheet = UIActionSheet(title: "ActionSheet", delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: "Done", otherButtonTitles: "Yes", "No")
actionSheet.showInView(self.view)
создать функцию листа действия
func actionSheet(actionSheet: UIActionSheet!, clickedButtonAtIndex buttonIndex: Int)
{
switch buttonIndex{
case 0:
NSLog("Done");
break;
case 1:
NSLog("Cancel");
break;
case 2:
NSLog("Yes");
break;
case 3:
NSLog("No");
break;
default:
NSLog("Default");
break;
//Some code here..
}
UIActionSheet
устарел с iOS8, я бы рекомендовал использовать UIAlertController
, если вам не нужно поддерживать версию ниже:
private func presentSettingsActionSheet() {
let settingsActionSheet: UIAlertController = UIAlertController(title:nil, message:nil, preferredStyle:UIAlertControllerStyle.ActionSheet)
settingsActionSheet.addAction(UIAlertAction(title:"Send Feedback", style:UIAlertActionStyle.Default, handler:{ action in
self.presentFeedbackForm()
}))
settingsActionSheet.addAction(UIAlertAction(title:"Tell Me a Joke!", style:UIAlertActionStyle.Default, handler:{ action in
self.presentRandomJoke()
}))
settingsActionSheet.addAction(UIAlertAction(title:"Cancel", style:UIAlertActionStyle.Cancel, handler:nil))
presentViewController(settingsActionSheet, animated:true, completion:nil)
}
Вот как это выглядит:
Обновлен для Swift 3:
Если вы хотите показать/открыть кнопку UIActionSheet на кнопке, нажмите ниже, используя простой и обновленный код в yourViewController:
//Определение метода:
func showPaymentModeActionSheet() {
// 1
let optionMenu = UIAlertController(title: nil, message: "Choose Payment Mode", preferredStyle: .actionSheet)
// 2
let fullAction = UIAlertAction(title: "FULL", style: .default, handler: {
(alert: UIAlertAction!) -> Void in
self.mPaymentModeTextField.text = "FULL"
})
let addvanceAction = UIAlertAction(title: "ADVANCE", style: .default, handler: {
(alert: UIAlertAction!) -> Void in
self.mPaymentModeTextField.text = "ADVANCE"
})
//
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: {
(alert: UIAlertAction!) -> Void in
})
// 4
optionMenu.addAction(fullAction)
optionMenu.addAction(addvanceAction)
optionMenu.addAction(cancelAction)
// 5
self.present(optionMenu, animated: true, completion: nil)
}
//Вызов метода:
@IBAction func actionOnPaymentModeButton(_ sender: Any) {
// open action sheet
showPaymentModeActionSheet()
}