Create the first project in Swift

introduction

The purpose of writing these is to learn and record, to facilitate quick search in the future, and to urge myself to learn something useful every day.

I won’t give examples for some too basic syntax, just start from creating a new project, and write a simple application!

1.file->project

     

2. Select app  

   

3. Select the Swift language and fill in the project name 

    

 3.1 The storyboard is chosen here, and the main SwiftUI has not been used, so it is not very good

  3.1.1 Project built files

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
}

3.1.2 Try to add a button to the main screen

Here are basically point calls, which look more concise than the oc language, and it is less difficult to use if you have an oc foundation.

   let button = UIButton.init(type: .custom)
               button.frame = CGRect(x: 100, y: 210, width: 40, height: 40)
               button.setTitle("添加", for: .normal)
               button.setTitleColor(.systemBlue, for: .normal)
               button.addTarget(self, action: #selector(clickPublishButton), for: .touchUpInside)
          
               self.view.addSubview(button)
        

Add a click event to the button

 
    @objc fileprivate func clickPublishButton() {
        print("这里打印一句话")
    }

Guess you like

Origin blog.csdn.net/hezhi66327/article/details/128832708