LoginSignup
11
11

More than 5 years have passed since last update.

[Swift] UIActionSheetの簡単なサンプル

Last updated at Posted at 2014-07-06

できること

ボタンを押すとUIActionSheetを表示して、選んだボタンのindexとタイトルを取得するだけです。

ソース

ViewController.swift
import UIKit

class ViewController: UIViewController, UIActionSheetDelegate {
    @IBAction func tapButton(sender : AnyObject) {
        var sheet: UIActionSheet = UIActionSheet()
        let title: String = "Please choose a plan"
        sheet.title  = title
        sheet.delegate = self
        sheet.addButtonWithTitle("Cancel")
        sheet.addButtonWithTitle("A plan")
        sheet.addButtonWithTitle("B plan")
        sheet.addButtonWithTitle("C plan")

        // キャンセルボタンのindexを指定
        sheet.cancelButtonIndex = 0

        // UIActionSheet表示
        sheet.showInView(self.view)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func actionSheet(sheet: UIActionSheet!, clickedButtonAtIndex buttonIndex: Int) {
        println("index %d %@", buttonIndex, sheet.buttonTitleAtIndex(buttonIndex))
    }
}
11
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
11