Swift UI – 操作表(UIActionSheet)

注:代码已升级至Swift4

创建操作表

创建一个操作表(或叫底部警告框)并弹出显示

注:自iOS 8起,建议使用UIAlertController代替UIActionSheet。点击查看UIAlertController的用法

import UIKit

class ViewController: UIViewController,UIActionSheetDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()

        let actionSheet=UIActionSheet()
        //actionSheet.title = "请选择操作"
        actionSheet.addButtonWithTitle("取消")
        actionSheet.addButtonWithTitle("动作1")
        actionSheet.addButtonWithTitle("动作2")
        actionSheet.cancelButtonIndex = 0
        actionSheet.delegate = self
        actionSheet.showInView(self.view);
    }

    func actionSheet(actionSheet: UIActionSheet, didDismissWithButtonIndex buttonIndex: Int) {
        print("点击了:" + actionSheet.buttonTitleAtIndex(buttonIndex)!)
    }
}

显示方式

在一般情况下,工具条,或标签条中显示的调用方式是不一样的

actionSheet.showInView(self.view);  //一般情况
actionSheet.showFromToolbar(self.navigationController.toolbar)  //工具条的情况下
actionSheet.showFromTabBar(self.tabBar)  //标签条的情况下
上一篇 Swift UI - 告警框(UIAlertView)
下一篇 Swift UI - 图像控件(UIImageView)
最新评论
一位WordPress评论者
一位WordPress评论者
2月12日
您好,这是一条评论。若需要审核、编辑或删除评论,请访问仪表盘的评论界面。评论者头像来自 Gravatar。