도래울

swift ActionSheet 예제 본문

개발/iOS

swift ActionSheet 예제

도래울 2016. 4. 21. 11:38

Open the Assistant Editor and make sure the ViewController.swift file is visible. Ctrl + drag from the Button to the ViewController class and create the following Action

implement the showActionSheet method

@IBAction func showActionSheet(sender: AnyObject) {
    // 1
    let optionMenu = UIAlertController(title: nil, message: "Choose Option", preferredStyle: .ActionSheet)
    
    // 2
    let deleteAction = UIAlertAction(title: "Delete", style: .Default, handler: {
      (alert: UIAlertAction!) -> Void in
      println("File Deleted")
    })
    let saveAction = UIAlertAction(title: "Save", style: .Default, handler: {
      (alert: UIAlertAction!) -> Void in
      println("File Saved")
    })
    
    //
    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: {
      (alert: UIAlertAction!) -> Void in
      println("Cancelled")
    })
    
    
    // 4
    optionMenu.addAction(deleteAction)
    optionMenu.addAction(saveAction)
    optionMenu.addAction(cancelAction)
    
    // 5
    self.presentViewController(optionMenu, animated: true, completion: nil)
  }
  1. an UIAlertController with the ActionSheet style is created
  2. two actions are created which can be added to the Alert Controller. Note the use of a closure inside the brackets of the handler parameter
  3. another action is created, this time with the Cancel style
  4. the actions are added to the Alert Controller
  5. The Alert Controller is presented.

Build and Run the project, click the button and select the different actions inside the Action Sheet.


Comments