简体   繁体   English

editButtonItem对象如何实例化?

[英]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.

相关问题 如何在 Swift 3 中设置 editButtonItem 标题 - How to set editButtonItem title in Swift 3 如何将自定义 UIimage 应用于 Swift 中的 editButtonItem? - How to apply custom UIimage to editButtonItem in Swift? 如何用按钮动作替换self.navigationItem.leftBarButtonItem = self.editButtonItem()} - how replace self.navigationItem.leftBarButtonItem = self.editButtonItem() } with button action editButtonItem不起作用 - editButtonItem Does Not Work 如何管理实例化的viewController - How to manage instantiated viewControllers 如何通过点符号访问未实例化的对象? (快速游乐场) - How can a non-instantiated object be accessed via dot notation? (Swift Playground) Swift:编辑模式,将editButtonItem()链接到IBAction - Swift: Edit Mode, link editButtonItem() to IBAction 如何指向尚未实例化的对象 - How to point to objects that aren't yet instantiated 有没有任何方法可以在没有任何实例化对象的情况下获取类的属性列表? - Is there any way to get the list of attributes of a class without any instantiated object? 类在实例化的对象中侦听事件的最佳方法是什么? (迅速) - What is the best way for a class to listen to events in an object it has instantiated? (Swift)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM