新建 StoryBoard 项目

新建工程 File>New>Project

在这里插入图片描述

选择 App 然后 next
在这里插入图片描述

写下自己项目名,这里我写 Test

在这里插入图片描述

界面选择 StoryBoard
在这里插入图片描述

StoryBoardcontroller绑定

Main.storyboard 的 View Controller 可以填写自定义title

在这里插入图片描述

这个 View Controller 绑定自己定义的 ViewController 类

在这里插入图片描述

与这里的 .swift 文件对应

在这里插入图片描述

配置解读

info.plist 文件存放一想工程配置,如下图

在这里插入图片描述

使用 UIKit 增加一个按钮

ViewController重载函数 viewDidLoad添加一个按钮,这种 #selector 的形式要使用 Object-C 的函数调用

let confirm = UIButton(frame: CGRect(x: 10, y: 40, width: 100, height: 40))
confirm.backgroundColor = .green
confirm.setTitle("He", for: .normal)
confirm.setTitleColor(.white, for: .normal)
confirm.addTarget(self, action: #selector(ViewController.press), for: .touchUpInside)
self.view.addSubview(confirm)

ViewController 中添加一objc函数,作为按钮点击响应事件

@objc func press() {
        let alertController = UIAlertController()
        alertController.title = "OK"
        alertController.message = "Hello World!!"
        let cancelAction = UIAlertAction(title: "No", style: .cancel)
        let confirmAction = UIAlertAction(title: "Yes", style: .default)
        alertController.addAction(cancelAction)
        alertController.addAction(confirmAction)
        self.present(alertController, animated: true, completion: nil)
        
        
    }

运行结果

在这里插入图片描述

原文地址:https://blog.csdn.net/weixin_39147809/article/details/125985668

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_36298.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注