Swift UI学习之UIAlertController(变得麻烦了)

简介:

在代码中使用了UIAlertView,由于没有智能的提示,编译没有问题,但当触发时,就会崩溃,而且没有

出现任何提示,不知道是什么错误,大家如果遇到同样的情况,就知道怎么解决了。

这里写了一个扩展,这样更方便使用了

import Foundation
import UIKit

extension UIAlertController {
    class func showAlert(
        presentController: UIViewController!,
        title: String!,
        message: String!,
        cancelButtonTitle: String? = "cancel",
        okButtonTitle: String? = "ok") {
        let alert = UIAlertController(title: title!, message: message!, preferredStyle: UIAlertControllerStyle.Alert)
        if cancelButtonTitle {
            alert.addAction(UIAlertAction(title: cancelButtonTitle!, style: UIAlertActionStyle.Default, handler: nil))// do not handle cancel, just dismiss
        }
        if okButtonTitle {
            alert.addAction(UIAlertAction(title: okButtonTitle!, style: UIAlertActionStyle.Default, handler: nil))// do not handle cancel, just dismiss
        }
        
        presentController!.presentViewController(alert, animated: true, completion: nil)
    }
    
    class func showAlert(
        presentController: UIViewController!,
        title: String!,
        message: String!,
        cancelButtonTitle: String? = "cancel",
        okButtonTitle: String? = "ok",
        okHandler: ((UIAlertAction!) -> Void)!) {
            let alert = UIAlertController(title: title!, message: message!, preferredStyle: UIAlertControllerStyle.Alert)
            if cancelButtonTitle {
                alert.addAction(UIAlertAction(title: cancelButtonTitle!, style: UIAlertActionStyle.Default, handler: nil))// do not handle cancel, just dismiss
            }
            if okButtonTitle {
                alert.addAction(UIAlertAction(title: okButtonTitle!, style: UIAlertActionStyle.Default, handler: okHandler))// do not handle cancel, just dismiss
            }
            
            presentController!.presentViewController(alert, animated: true, completion: nil)
    }
}


调用来测试

    func onButtonClick(sender: UIButton!) {
        println("clicked button")

       // UIAlertController.showAlert(self, title: "title", message: "message")
       // UIAlertController.showAlert(self, title: "title", message: "message", cancelButtonTitle: "cancel", okButtonTitle: "ok")
        UIAlertController.showAlert(self, title: "title", message: "message", cancelButtonTitle: "cancel", okButtonTitle: "ok", okHandler: {
            (UIAlertAction) in
            println("no nothing")
        })
        // do not use below
        //alert.show()
        // create UIAlertView
//        let alertView = UIAlertView(title: "alertView", message: "clickedButton event", delegate: self, cancelButtonTitle: "cancel", otherButtonTitles: "ok")
//        alertView.show()
    }



目录
相关文章
|
2月前
SAP UI5 Link 控件的使用方法介绍 - 后续学习 Fiori Elements Smart Link 的基础试读版
SAP UI5 Link 控件的使用方法介绍 - 后续学习 Fiori Elements Smart Link 的基础试读版
15 0
|
3月前
|
JSON 监控 数据格式
Easy UI datagrid的学习
Easy UI datagrid的学习
|
5月前
|
JavaScript
【Vue学习】—Vue UI组件库(二十八)
【Vue学习】—Vue UI组件库(二十八)
|
6月前
|
XML 数据安全/隐私保护 数据格式
Morn UI 学习总结
Morn UI 学习总结
47 0
|
7月前
|
Web App开发 开发者
关于 SAP UI5 学习教程示例代码里 Chrome 开发者工具 Console 面板里一些错误消息的说明试读版
关于 SAP UI5 学习教程示例代码里 Chrome 开发者工具 Console 面板里一些错误消息的说明试读版
38 0
|
7月前
|
XML 开发框架 数据格式
SAP UI5 应用开发教程之五十七 - 基于 OData 注解的 Smart Field 使用方法学习试读版
SAP UI5 应用开发教程之五十七 - 基于 OData 注解的 Smart Field 使用方法学习试读版
47 0
SAP UI5 应用开发教程之五十七 - 基于 OData 注解的 Smart Field 使用方法学习试读版
|
8月前
|
存储 JavaScript API
通过 SAP UI5 的 TypeScript 开发环境,来学习什么是 DefinitelyTyped
通过 SAP UI5 的 TypeScript 开发环境,来学习什么是 DefinitelyTyped
57 0
|
9月前
|
前端开发 JavaScript
【编写前端需要学习的知识】Vue2+Element-UI
【编写前端需要学习的知识】Vue2+Element-UI
54 0
|
9月前
|
存储 数据管理 数据库
CoreData 在 swift UI 中的使用
当谈到在 Swift UI 中进行本地数据持久化,Core Data 是一个强大且常用的解决方案。Core Data 是苹果提供的一种数据存储和管理框架,用于在应用程序中创建、读取、更新和删除数据。它提供了一个对象图管理器,可以将数据映射到对象,并提供了一种简化数据操作的方式。在本文中,我们将深入介绍 Core Data 在 Swift UI 中的使用。
CoreData 在 swift UI 中的使用
|
9月前
|
开发框架 JavaScript 前端开发
HarmonyOS学习路之开发篇—Java UI框架(使用工具自动生成JS FA调用PA代码)
JS FA(Feature Ability)调用PA (Particle Ability)是使用基于JS扩展的类Web开发范式的方舟开发框架所提供的一种跨语言能力调用的机制,用于建立JS能力与Java能力之间传递方法调用、处理数据返回以及订阅事件上报的通道。开发者可以使用FA调用PA机制进行应用开发,但直接使用该机制需要开发者手动撰写大量模板代码,且模板代码可能与业务代码相互耦合,使得代码可维护性和可读性较差。

相关课程

更多