2014-06-11 86 views
10

我創建了一個動作片,但問題是,委託方法是不是叫UIActionSheet與SWIFT

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) 

任何想法來解決這個問題?

+4

動作片和警報視圖被折舊在IOS 8和alertController被引入。 – nikhil84

+3

是的,它的棄用,但如果你支持iOS 7的應用程序,那麼alertController將無法正常工作。所以更好地檢查iOS版本併爲iOS 8和iOS 7調用適當的代碼。 –

+0

看看這個答案 - http://stackoverflow.com/questions/27787777/how-to-create-uiactionsheet-actions/27798750#27798750 – Kampai

回答

8

你從來沒有動作片的代表:

myActionSheet = UIActionSheet() 
myActionSheet.delegate = self 
20

UIActionSheet在迅速語言: -

動作片材cancelButton和destructiveButton

設置UIActionSheetDelegate。因爲iOS8上與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.. 

} 
+0

我寫了一個Swift版本,不依賴於硬編碼按鈕索引的另一個答案:http://stackoverflow.com/a/29272140/37168 – stone

+0

Goooood Gooooood:P –

14

UIActionSheet

 let actionSheet = UIActionSheet(title: "ActionSheet", delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: "Done") 
     actionSheet.showInView(self.view) 

行動表上已經過時,我會建議使用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) 
} 

這裏是什麼樣子提出:

                                                                          AlertViewController in Swift

0

更新了斯威夫特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) 
} 

//方法調用: