简体   繁体   English

如何以编程方式向 UIBarButton 添加操作?

[英]How to add an action to a UIBarButton programmatically?

I've been creating a small iOS app using Swift just for fun, and I have already decided that I want a notification box (a bell-shaped button you can click on to check if there's any notification), and I also wanted to add the bell-shaped button to every screen.我一直在使用 Swift 创建一个小的 iOS 应用程序只是为了好玩,我已经决定我想要一个通知框(一个钟形按钮,你可以点击它来检查是否有任何通知),我还想添加每个屏幕的钟形按钮。
So, I decided to make a base view controller and have other view controllers inherit it.所以,我决定制作一个基本视图控制器并让其他视图控制器继承它。 However, that's when my problem arose;然而,这正是我的问题出现的时候; I have no idea how to add an action func for that button.我不知道如何为该按钮添加动作功能。 Since I create the bell-shaped button programmatically, I cannot just ^ drag and create a new IBaction.由于我以编程方式创建了钟形按钮,因此我不能只是^ drag并创建一个新的 IBaction。

I found this post: link , but this is for a UIButton, not for a UIBarButton, and it didn't work for me.我找到了这篇文章: link ,但这是针对 UIButton,而不是针对 UIBarButton,它对我不起作用。

Sorry for this long question.抱歉问了这么长的问题。 Below is a simple, one-sentenced question:下面是一个简单的单句问题:
MY PROBLEM我的问题
How can I add an action to a UIBarButton programmatically?如何以编程方式向 UIBarButton 添加操作?

UPDATE Here's my base view controller:更新这是我的基本视图控制器:

import UIKit

class BaseViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // add a notification button
        let notificationButton = UIBarButtonItem(image: UIImage(systemName: "bell.fill"))
        notificationButton.tintColor = .black
        
        self.navigationItem.rightBarButtonItem = notificationButton
    }
    
    
    
}

UPDATE2更新2

Here's my new code:这是我的新代码:

import UIKit

class BaseViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // add a notification button
        let notificationButton = UIBarButtonItem(
            image: UIImage(systemName: "bell.fill"),
            style: .plain,
            target: self,
            action: #selector(notificationButtonPressed)
        )
        
        notificationButton.tintColor = .black
        
        self.navigationItem.rightBarButtonItem = notificationButton
    }
    
    @objc func notificationButtonPressed() {
        print("Hello")
    }
}

You can pass a target-action pair to the initialiser of UIBarButtonItem :您可以将目标操作对传递给UIBarButtonItem的初始化UIBarButtonItem

let barButton = UIBarButtonItem(
    image: UIImage(systemName: "bell.fill"), 
    style: .plain, 
    target: self, action: #selector(buttonTapped)
)

// somewhere in your view controller:

@objc func buttonTapped() {
    // do something when the bar button is tapped
}

See the documentation here .请参阅此处的文档。

This is similar to UIButton 's addTarget(_:action:for:_) method, if you are familiar with that.这类似于UIButtonaddTarget(_:action:for:_)方法,如果你熟悉的话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM