关于ios:Xcode-建立-UIKit-项目Hello-World

6次阅读

共计 1201 个字符,预计需要花费 4 分钟才能阅读完成。

Xcode 建设 UIKit 我的项目(Hello World)

新建 StoryBoard 我的项目

新建工程 File>New>Project

抉择 App 而后 next

写下本人的我的项目名,这里我写 Test

界面抉择 StoryBoard

StoryBoard 与 controller 的绑定

Main.storyboard 的 View Controller 能够填写自定义的 title

将这个 View Controller 绑定到本人定义的 ViewController 类

与这里的 .swift 文件对应

配置解读

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

  • Delegate Name 是放代理的名字,这里它默认为:$(PRODUCT_MODULE_NAME).SceneDelegate, 与左侧的 SceneDelegate 对应。SceneDelegate 用于解决与 UI 相干的内容。
  • StoryBoard Name 寄存次要故事板名字,它默认为 Main,与左侧的 Main.storyboard 文件对应。

应用 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)
        
        
    }

运行后果

正文完
 0