Swift之使用UIAlertController實現UIActionsheet

來源:互聯網
上載者:User

標籤:ios開發   swift   

在iOS8的UIActionSheet被廢棄,我們在實現UIActionsheet時會選擇用UIAlertController來實現。本篇博文將會實現UIAlertView實現UIactionSheet效果。

具體步驟:

1、建立一個ActionSheet類型的UIAlertController;

2、為1中建立的UIAlertController建立兩個方法;本例中建立一個添加方法:addAction()和一個刪除方法deleteAction();

3、建立一個取消方法,參數style類型為Cancel;

4、將2和3步驟中建立的方法添加給1中建立的UIAlertController;

5、將1中建立的UIAlertContrller使用模態視圖推出。


具體代碼實現如下:

</pre><pre name="code" class="html">@IBAction func showActionSheet(sender: AnyObject) {        println("show action sheet")        let optionMenu = UIAlertController(title: nil, message: "選擇", preferredStyle: .ActionSheet)        let deleteAction = UIAlertAction(title: "刪除", style: .Default, handler:{ (alert: UIAlertAction!) -> Void in            println("刪除")        })                let saveAction = UIAlertAction(title: "儲存", style: .Default, handler: { (alert: UIAlertAction!) -> Void in            println("儲存")                    })                let cancelAction = UIAlertAction(title: "取消", style: .Cancel, handler: { (alert: UIAlertAction!) -> Void in            println("取消")                    })                optionMenu.addAction(saveAction)        optionMenu.addAction(deleteAction)        optionMenu.addAction(cancelAction)                self.presentViewController(optionMenu, animated: true, completion: nil)    }


效果如下:



註:最近在結合國外swift開發網站學習swift,堅持每天將自己學的swift翻譯並以部落格形式寫出來。


Swift之使用UIAlertController實現UIActionsheet

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.