gpt4 book ai didi

ios - Swift,与 UIBarButtonItem 混淆

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:08:43 26 4
gpt4 key购买 nike

我最近开始学习 Swift。当我尝试制作我的第一个 App 时,我对 UIBarButtonItem 感到困惑。如果我将 UIBarButtonItem 初始化放在 viewDidLoad() 函数之外,当我按下 Next 按钮时什么也不会发生。

class ViewController: UIViewController, UITextFieldDelegate {
let rightBarButton: UIBarButtonItem = UIBarButtonItem(title: "Next", style: .plain, target: self, action: #selector(onClickNext(button:)))

override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white

self.navigationItem.rightBarButtonItem = rightBarButton
}

func onClickNext(button: UIBarButtonItem) {
print("should push view controller")
}
}

但是,当我将initialization 放入viewDidLoad() 函数时,输出区域会输出我在onClickNext(button: ) 函数。

class ViewController: UIViewController, UITextFieldDelegate {
var rightBarButton: UIBarButtonItem?

override func viewDidLoad() {
super.viewDidLoad()

self.view.backgroundColor = .white

self.rightBarButton = UIBarButtonItem(title: "Next", style: .plain, target: self, action: #selector(onClickNext(button:)))
self.navigationItem.rightBarButtonItem = rightBarButton
}

func onClickNext(button: UIBarButtonItem) {
print("should push view controller")
}
}

此外,我发现当我将 initialization 放在 viewDidLoad() 函数之外时,我将 UITextField 添加到 viewController,如果我在按下按钮之前触摸 textfieldrightBarButton 就会工作。

这让我很困惑。机制是什么?

最佳答案

好吧,也许您不知道 ViewController 在内部是如何工作的。

首先,viewDidLoad 是您通常设置或初始化任何 View 或 View 属性 的区域。此方法在 View Controller 对象的生命周期中也只被调用一次。这意味着 self 已经存在。

了解这一点对于理解 let 属性的作用很重要,(来自 Apple)

A constant declaration defines an immutable binding between the constant name and the value of the initializer expression; after the value of a constant is set, it cannot be changed. That said, if a constant is initialized with a class object, the object itself can change, but the binding between the constant name and the object it refers to can’t.

尽管上部区域是您声明变量和常量的地方,通常用于简单的初始化,但它只是告诉 VC 有一个您想要使用的对象并将具有类全局范围的区域,但是当加载 View 层次结构时将添加其余功能(意味着该对象不依赖于自身,例如,将目标添加到按钮时,您指的是自身内部的方法)......这变量或常量称为存储属性

In its simplest form, a stored property is a constant or variable that is stored as part of an instance of a particular class or structure. Stored properties can be either variable stored properties (introduced by the var keyword) or constant stored properties (introduced by the let keyword).

最后,您有一个惰性存储属性,它也许可以用于您想要的:

A lazy stored property is a property whose initial value is not calculated until the first time it is used. You indicate a lazy stored property by writing the lazy modifier before its declaration.

解决方案:创建一个 lazy var 存储属性或者在 ViewDidLoad 中添加他的属性(当 self 已经存在时)

lazy private var doneButtonItem : UIBarButtonItem = {
[unowned self] in
return UIBarButtonItem(title: "Next", style:UIBarButtonItemStyle.Plain, target: self, action: #selector(onClickNext(button:)))
}()

let rightBarButton: UIBarButtonItem?

override func viewDidLoad() {
super.viewDidLoad()
rightBarButton = UIBarButtonItem(title: "Next", style: .plain, target: self, action: #selector(onClickNext(button:)))
}

关于ios - Swift,与 UIBarButtonItem 混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45408498/

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