gpt4 book ai didi

ios - UIView Controller 实例化

转载 作者:行者123 更新时间:2023-11-28 07:56:20 25 4
gpt4 key购买 nike

我正在尝试以编程方式实例化 UIView 和 UIViewController,但在显示我的任何标签、文本字段等时遇到问题。

我的 View Controller 看起来像这样:

import UIKit

class AdminNewsController: UIViewController {

var adminNewsView : AdminNewsView!


let newsTitleMarker : UILabel = {
let label = UILabel()
label.text = "Title: "
label.font.withSize(AMA_Constants.FontSizes.HeadingSize)
label.textColor = AMA_Constants.FontColor.LabelMarkingColor
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()

let newsTitleTextEdit : UITextField = {
let textEdit = UITextField()
textEdit.text = "Text View"
textEdit.textColor = AMA_Constants.FontColor.LabelMarkingColor
textEdit.textAlignment = .center
textEdit.translatesAutoresizingMaskIntoConstraints = false
return textEdit
}()

override func viewDidLoad() {
super.viewDidLoad()

adminNewsView = AdminNewsView()
adminNewsView.backgroundColor = AMA_Constants.AppPrimaryColor
self.view.addSubview(adminNewsView)

setupViews()

navigationController?.navigationBar.barStyle = .black
navigationController?.navigationBar.tintColor = AMA_Constants.AppSecondaryColor
self.navigationItem.title = "Admin - News"

}


func setupViews() {

adminNewsView.addSubview(newsTitleMarker)
adminNewsView.addSubview(newsTitleTextEdit)


adminNewsView.translatesAutoresizingMaskIntoConstraints = false
adminNewsView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
adminNewsView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
adminNewsView.widthAnchor.constraint(equalTo: self.view.widthAnchor).isActive = true
adminNewsView.heightAnchor.constraint(equalTo: self.view.heightAnchor).isActive = true

newsTitleMarker.anchor(adminNewsView.topAnchor, left: adminNewsView.leftAnchor, bottom: nil, right: nil, topConstant: AMA_Constants.Alignment_Space.paddingVerticalExternal, leftConstant: AMA_Constants.Alignment_Space.paddingHorizontalExternal, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)



newsTitleTextEdit.anchor(newsTitleMarker.topAnchor, left: newsTitleMarker.rightAnchor, bottom: nil, right: adminNewsView.rightAnchor, topConstant: 0, leftConstant: AMA_Constants.Alignment_Space.paddingHorizontalInternal, bottomConstant: 0, rightConstant: AMA_Constants.Alignment_Space.paddingHorizontalExternal, widthConstant: 0, heightConstant: 0)

}}

但在模拟器中, View 是正确的(白色)颜色,但没有标签或 UITextView。

问题:

  1. 我是否正确实例化并创建了 View adminNewsView?
  2. 为什么不生成标签和 UITextView?
  3. 没有完全理解 IOS 上的 MVC,我的 adminNewsView 类应该写些什么?我以为我应该在其中包含我的 setupViews() 函数,但事实并非如此。

最佳答案

我对您的代码有一些问题,所以我以一种我认为有助于更好地为您说明编程 View Controller 的过程的方式重构它。其中一个问题是您从未创建 View Controller 的 View ,这对于编程 View Controller 是强制性的。

我强烈建议像书一样编写代码——从左到右,从上到下。首先声明所有需要成为实例属性的属性,然后调用所有生命周期事件(我不会在生命周期方法中放置任何不是方法调用的代码,因为它读起来容易得多),然后编写那些方法出来了。

import UIKit

class AdminNewsController: UIViewController {


// MARK: Properties


let adminNewsView = AdminNewsView()
let newsTitleMarker = UILabel()
let newsTitleTextEdit = UITextField()


// MARK: Lifecycle


override func loadView() {

// do NOT call super.loadView() when programmatically creating a
// view controller

// do all of your basic setup here in loadView

setView()
addAdminNewsView()
addNewsTitleMarker()
addNewsTitleTextEdit()

}

override func viewDidLoad() {
super.viewDidLoad()

// do call super.viewDidLoad() however
// do post-basic setup stuff here in viewDidLoad
// viewDidLoad is called after the entire view controller has been
// loaded into memory so consider that

}


// other lifecycle events that come after viewDidLoad where you can do some
// last-second touches include viewDidLayoutSubviews(), viewDidAppear(), etc.


deinit {

// do any cleanup here like deactivating timers, removing observers, etc.

}


// MARK: Methods


func setView() {

view = UIView()
view.frame = UIScreen.main.bounds
view.backgroundColor = UIColor.white

}


func addAdminNewsView() {

adminNewsView.backgroundColor = AMA_Constants.AppPrimaryColor
adminNewsView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(adminNewsView)
adminNewsView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
adminNewsView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
adminNewsView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
adminNewsView.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true

}


func addNewsTitleMarker() {

newsTitleMarker.text = "Title: "
newsTitleMarker.font.withSize(AMA_Constants.FontSizes.HeadingSize)
newsTitleMarker.textColor = AMA_Constants.FontColor.LabelMarkingColor
newsTitleMarker.textAlignment = .center
newsTitleMarker.translatesAutoresizingMaskIntoConstraints = false
adminNewsView.addSubview(newsTitleMarker)

// add newsTitleMarker constraints

}


func addNewsTitleTextEdit() {

newsTitleTextEdit.text = "Text View"
newsTitleTextEdit.textColor = AMA_Constants.FontColor.LabelMarkingColor
newsTitleTextEdit.textAlignment = .center
newsTitleTextEdit.translatesAutoresizingMaskIntoConstraints = false
adminNewsView.addSubview(newsTitleTextEdit)

// add newsTitleTextEdit constraints

}


}

既然你问了,下面是子类化的 UIView 的样子。请注意不同的生命周期方法。

class SomeView: UIView {


// MARK: Properties


private let itemBanner = UIImageView()
private let itemLabel = UILabel()
var itemName: String
var itemPicture: UIImage


// MARK: Lifecycle


init(name: String, picture: UIImage) {

self.itemName = name
self.itemPicture = picture
super.init(frame: CGRect.zero)
config()
addItemBanner()
addItemLabel()

}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}


override func updateConstraints() {
super.updateConstraints()

setConstraints()

}


override func layoutSubviews() {
super.layoutSubviews()

// this can be a very useful method when subclassing views

}


// MARK: Methods


private func config() {

isOpaque = false

}


private func addItemBanner() {

itemBanner.image = itemPicture
itemBanner.clipsToBounds = true
itemBanner.contentMode = .scaleAspectFill
itemBanner.translatesAutoresizingMaskIntoConstraints = false
addSubview(itemBanner)

}


private func addItemLabel() {

itemLabel.text = itemName
itemLabel.textAlignment = .left
itemLabel.numberOfLines = 3
itemLabel.lineBreakMode = .byTruncatingTail
itemLabel.font = UIFont.bigTitle
itemLabel.translatesAutoresizingMaskIntoConstraints = false
itemBanner.addSubview(itemLabel)

}


private func setConstraints() {

// banner
itemBanner.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
itemBanner.topAnchor.constraint(equalTo: topAnchor).isActive = true
itemBanner.widthAnchor.constraint(equalTo: widthAnchor).isActive = true
itemBanner.heightAnchor.constraint(equalToConstant: 96).isActive = true
itemBanner.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true

// label
itemLabel.centerXAnchor.constraint(equalTo: itemBanner.centerXAnchor).isActive = true
itemLabel.centerYAnchor.constraint(equalTo: itemBanner.centerYAnchor).isActive = true
itemLabel.widthAnchor.constraint(equalTo: itemBanner.widthAnchor, constant: -32).isActive = true
itemLabel.sizeToFit()

}


}

关于ios - UIView Controller 实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47944792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com