ios - 在 UIBarItem 点击时未调用委托(delegate)

标签 ios iphone swift delegates

点击 UIBarItem 时不会调用委托(delegate)方法。我经历了很多建议,所有的建议都和我写的一样。

我按照建议试了,没有解决办法。到处给委托(delegate)打电话。我做了每件事。

class ViewController: CommonClass,Mydelegate {

    var vc = CommonClass()

    override func viewDidLoad() {
        super.viewDidLoad()
        vc = CommonClass(nibName: "CommonClass", bundle: nil)
        initializeCartBarButton()
        vc.delegate = self
        print( (vc.delegate))
    }

    func testing() {
        print("hello")
    }
}

class CommonClass: UIViewController {

    var delegate:Mydelegate?

    func initializeCartBarButton() {
        let cartBarButton = UIBarButtonItem()
        cartBarButton.title = "cart"
        cartBarButton.target = self
        cartBarButton.action = Selector("goToCart")
        self.navigationItem.rightBarButtonItem = cartBarButton;

    }

    func goToCart() {
        print("hi")
        if self.delegate?.testing() != nil {
            self.delegate?.testing()

        }else {
            print(self.delegate)
        }
    }
}

最佳答案

我看到的是:

  1. CommonClass 继承UIViewController
  2. ViewController 继承CommonClass
  3. 意味着 View Controller 可以直接调用 CommonClass 方法。
  4. 您可以从 ViewController 直接调用 goToCart() 方法。

因此,无需使用委托(delegate)模式来调用CommonClass的方法。

关于ios - 在 UIBarItem 点击时未调用委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39484506/

相关文章:

ios - 跟踪类别的 SKNodes

ios - AudioBufferList 在 Swift 中分配

iphone - 如何使用 UIAlertView 生成多行而不是单行?

iphone - 如何在 UINavigationController 的 RootViewController 上显示后退按钮?

ios - 为什么我的按钮无法从应用程序推送到 Apple map ?

ios - 以编程方式将选项卡栏添加到 ViewController

ios - Swift - tableView 行高仅在滚动或切换展开/折叠后更新

ios - 如何添加 pod 0.3.3

ios - iOS 中的静态页脚

iphone - iPhone 中的声音触控