Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

swift - How to arrange a view in architecture MVC? When coding through code

I am studying architecture MVC and I write the interface through code. I am trying to link a VIEW and a CONTROLLER. But it doesn't work. What's my mistake?

Controller:

import UIKit

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        ViewExample.setupUI()
    }
    
}

View:

import UIKit

class ViewExample: UIView {
    
    static func setupUI() {
        
        let view = UIView()

        let labelTitle: UILabel = {
            let label = UILabel()
            label.translatesAutoresizingMaskIntoConstraints = false
            label.text = "Hello, world!"
            
            return label
        }()
        
        view.backgroundColor = .white
        view.addSubview(labelTitle)
        
        NSLayoutConstraint.activate([
            labelTitle.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 10),
            labelTitle.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10),
            labelTitle.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10),
            
        ])
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

I understand why a static is needed. Why doesn't it work for me? I have not found any good example on github. Therefore, I have to turn to you


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I don't see why you would use static, you want to set up the UI of a UIView's instance, thus no need to have a static class method. In your ViewExample:

import UIKit

class ViewExample: UIView {

  func setupUI() {

    let labelTitle: UILabel = {
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        label.text = "Hello, world!"
        
        return label
    }()
    
    self.backgroundColor = .white
    self.addSubview(labelTitle)
    
    NSLayoutConstraint.activate([
        labelTitle.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor, constant: 10),
        labelTitle.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 10),
        labelTitle.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -10),
        
    ])
    
}

override init(frame: CGRect){
    super.init(frame: frame)
    setupUI()
}

required init?(coder: NSCoder) {
    super.init(coder: coder)
    setupUI()
}

}

In your ViewController

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let view = ViewExample(frame: (x: 0, y: 0, width: 200, height: 50)) //Change the frame according to where you want to position your view
         self.addSubview(view)
      }

   }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...