2015-01-05 42 views
1

我知道我必須設置代表(我是這樣做的:class ShiftOverview: UITableViewController, UIActionSheetDelegate {...),但是當我點擊按鈕時,我仍然沒有得到任何迴應。如何創建UIActionSheet操作?

它看起來並不像您可以設置功能中的委託與UIAlertController要麼...

@IBAction func takeShift(sender: AnyObject) { 

    let myActionSheet = UIAlertController (title: "Confirm", message: "Test message", preferredStyle: UIAlertControllerStyle.ActionSheet) 

    let actionOne = UIAlertAction (title: "Take Shift", style: .Default, handler: nil) 
    let actionTwo = UIAlertAction (title: "View ESA", style: .Default, handler: nil) 

    let actionCancel = UIAlertAction (title: "Cancel", style: .Cancel, handler: nil) 

    myActionSheet.addAction(actionOne) 
    myActionSheet.addAction(actionTwo) 
    myActionSheet.addAction(actionCancel) 

    self.presentViewController(myActionSheet, animated: true, completion: nil) 
} 

func actionSheet (myActionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) 
{ 
    switch buttonIndex { 

    case 0: 
     println ("test0") 
     break 
    case 1: 
     println ("test1") 
     break 
    case 2: 
     println ("test2") 
     break 

    default: 
     println("nope") 

    } 
} 

回答

0

到iOS 8之前,UIAlertViews和UIActionSheets是分開控制。然而,在iOS 8中,一個新類UIAlertController將這兩個控件組合成一個簡單易用的類。

而不是像以前那樣使用這些控件的委託模式,你現在傳遞一個閉包來調用。您有handler等於nil的地方是您放置代碼的地方。

這很可能是因爲Swift把封閉視爲一等公民,而Objective C沒有那麼多(帶塊)。

它應該是:

@IBAction func takeShift(sender: AnyObject) { 

    let myActionSheet = UIAlertController (title: "Confirm", message: "Test message", preferredStyle: UIAlertControllerStyle.ActionSheet) 

    let actionOne = UIAlertAction (title: "Take Shift", style: .Default, handler: { (action) in 
     println("test0") 
    }) 
    let actionTwo = UIAlertAction (title: "View ESA", style: .Default, handler: { (action) in 
     println("test1") 
    }) 

    let actionCancel = UIAlertAction (title: "Cancel", style: .Cancel, handler: { (action) in 
     println("test2") 
    }) 

    myActionSheet.addAction(actionOne) 
    myActionSheet.addAction(actionTwo) 
    myActionSheet.addAction(actionCancel) 

    self.presentViewController(myActionSheet, animated: true, completion: nil) 
} 

瞭解更多關於這NSHipster's article on UIAlertControllers或其documentation

+0

感謝您的幫助:) – dom999999

0

而不是將nil傳遞給您的處理程序,您可以編寫要執行的操作。您不再需要委託方法。

let actionOne = UIAlertAction (title: "Take Shift", style: .Default){ (action) -> Void in 
     println ("Take Shift") 
    }; 
let actionTwo = UIAlertAction (title: "View ESA", style: .Default){ (action) -> Void in 
     println ("View ESA") 
    }; 

let actionCancel = UIAlertAction (title: "Cancel", style: .Cancel){ (action) -> Void in 
     println ("Cancel") 
    }; 
0

你可能喜歡也看到這一點:

func AlertViewWithActionsheet() { 
     let alert = UIAlertController(title: "Alert with actionsheet", message: "Alert with actionsheet", preferredStyle: UIAlertControllerStyle.ActionSheet); 

     let dismissHandler = { 
      (action: UIAlertAction!) in 
       // This is an action event - called when you press OK button. 
       self.dismissViewControllerAnimated(true, completion: {() -> Void in 
      }) 
     } 

     // add action 
     alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: dismissHandler)) 

     presentViewController(alert, animated: true) {() -> Void in 
     } 
    }