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()
    }



目录
相关文章
|
人工智能 物联网 API
LLM 大模型学习必知必会系列(十三):基于SWIFT的VLLM推理加速与部署实战
LLM 大模型学习必知必会系列(十三):基于SWIFT的VLLM推理加速与部署实战
LLM 大模型学习必知必会系列(十三):基于SWIFT的VLLM推理加速与部署实战
|
测试技术 Swift iOS开发
探索iOS自动化测试:使用Swift编写UI测试
【8月更文挑战第31天】在软件开发的海洋中,自动化测试是保证船只不偏离航线的灯塔。本文将带领读者启航,深入探索iOS应用的自动化UI测试。我们将通过Swift语言,点亮代码的灯塔,照亮测试的道路。文章不仅会展示如何搭建测试环境,还会提供实用的代码示例,让理论知识在实践中生根发芽。无论你是新手还是有经验的开发者,这篇文章都将是你技能提升之旅的宝贵指南。
|
JSON 监控 数据格式
Easy UI datagrid的学习
Easy UI datagrid的学习
484 0
|
存储 数据管理 数据库
CoreData 在 swift UI 中的使用
当谈到在 Swift UI 中进行本地数据持久化,Core Data 是一个强大且常用的解决方案。Core Data 是苹果提供的一种数据存储和管理框架,用于在应用程序中创建、读取、更新和删除数据。它提供了一个对象图管理器,可以将数据映射到对象,并提供了一种简化数据操作的方式。在本文中,我们将深入介绍 Core Data 在 Swift UI 中的使用。
CoreData 在 swift UI 中的使用
|
JavaScript
【Vue学习】—Vue UI组件库(二十八)
【Vue学习】—Vue UI组件库(二十八)
|
XML 数据安全/隐私保护 数据格式
Morn UI 学习总结
Morn UI 学习总结
360 0
|
前端开发 JavaScript
【编写前端需要学习的知识】Vue2+Element-UI
【编写前端需要学习的知识】Vue2+Element-UI
333 0
|
开发框架 JavaScript 前端开发
HarmonyOS学习路之开发篇—Java UI框架(使用工具自动生成JS FA调用PA代码)
JS FA(Feature Ability)调用PA (Particle Ability)是使用基于JS扩展的类Web开发范式的方舟开发框架所提供的一种跨语言能力调用的机制,用于建立JS能力与Java能力之间传递方法调用、处理数据返回以及订阅事件上报的通道。开发者可以使用FA调用PA机制进行应用开发,但直接使用该机制需要开发者手动撰写大量模板代码,且模板代码可能与业务代码相互耦合,使得代码可维护性和可读性较差。
|
开发框架 JavaScript 前端开发
HarmonyOS学习路之开发篇—Java UI框架(JS FA调用Java PA)
使用兼容JS的类Web开发范式的方舟开发框架提供了JS FA(Feature Ability)调用Java PA(Particle Ability)的机制,该机制提供了一种通道来传递方法调用、处理数据返回以及订阅事件上。
|
XML Java 数据格式
HarmonyOS学习路之开发篇—Java UI框架(动画开发)
动画开发 动画是组件的基础特性之一,精心设计的动画使UI变化更直观,有助于改进应用程序的外观并改善用户体验。Java UI框架提供了帧动画、数值动画和属性动画,并提供了将多个动画同时操作的动画集合。

热门文章

最新文章

相关课程

更多