[英]How is the editButtonItem object instantiated?
I'm following a tutorial on using the Keychain and I prefer to understand how the code works before diving into the tutorial. 我正在关注使用钥匙串的教程,并且更喜欢在开始学习教程之前先了解代码的工作原理。 I noticed that in this class called MasterViewController, it assigns a UIBarButtonItem with a style of edit to the navigationItem.leftBarButtonItem variable.
我注意到,在名为MasterViewController的此类中,它向NavigationItem.leftBarButtonItem变量分配了一种具有编辑样式的UIBarButtonItem。 The code doesn't instantiate this edit button anywhere in the codebase so I'm curious to know how does this work?
代码没有在代码库中的任何地方实例化此编辑按钮,因此我很好奇这是如何工作的? When I try to do the following, it doesn't work:
当我尝试执行以下操作时,它不起作用:
navigationItem.rightBarButtonItem = camera
Why is this? 为什么是这样?
The tutorial I'm following is found here on the Ray Weinerlich Website : 我正在关注的教程可以在Ray Weinerlich网站上找到:
Here is the code for the MasterViewController: 这是MasterViewController的代码:
class MasterViewController: UIViewController, UITableViewDelegate {
// MARK: - IBOutlets
@IBOutlet var tableView: UITableView!
// MARK: - Properties
var detailViewController: DetailViewController?
var managedObjectContext: NSManagedObjectContext?
var isAuthenticated = false
var didReturnFromBackground = false
var _fetchedResultsController: NSFetchedResultsController<Note>?
// MARK: - View Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = editButtonItem
let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(insertNewObject(_:)))
navigationItem.rightBarButtonItem = addButton
if let split = splitViewController {
let controllers = split.viewControllers
detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? DetailViewController
}
}
It's a built-in UIBarButton
item available readily for users to use. 这是内置的
UIBarButton
项,用户可以随时使用。 You can directly use it and it'll toggle between edit and done. 您可以直接使用它,它将在编辑和完成之间切换。
See the documentation below. 请参阅下面的文档。
If you have a doubt with an object or variable, just Command + click on that variable and object and you could check more about it. 如果您对某个对象或变量有疑问,只需按住Command键并单击该变量和对象,即可查看更多有关该对象的信息。
I think you are asking about this line: 我想您是在问这条线:
navigationItem.leftBarButtonItem = editButtonItem
editButtonItem
is from UIViewController
. editButtonItem
来自UIViewController
。 Note that this is a read-only property. 请注意,这是一个只读属性。 The implementation is probably a lazy computed property that creates the button the first time you access the property.
该实现可能是一个惰性计算属性,该属性在您首次访问该属性时创建按钮。 It's part of the
UIViewController
implementation. 它是
UIViewController
实现的一部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.