首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在UIBarItem点击时未调用委派

在UIBarItem点击时未调用委派
EN

Stack Overflow用户
提问于 2016-09-14 15:13:32
回答 2查看 65关注 0票数 1

委派方法不调用点击UIBarItem。我经历了这么多的建议,都是我写的一样的。

我根据建议尝试了,没有解决方案。在给定的任何地方调用委托。我做了每一件事。

代码语言:javascript
复制
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)
        }
    }
}
EN

回答 2

Stack Overflow用户

发布于 2016-09-14 15:28:09

我看到的是:

  1. CommonClass继承UIViewController.
  2. ViewController继承CommonClass.
  3. Means视图控制器可以直接调用ViewController.

中的CommonClass方法。

  • 您可以直接从View中调用goToCart()方法

因此,不需要使用委托模式来调用CommonClass的方法。

票数 0
EN

Stack Overflow用户

发布于 2016-09-14 16:59:21

演示使用自定义委托的示例示例:

ViewController.swift

代码语言:javascript
复制
import UIKit


protocol Mydelegate
{
    func testing()
}

class ViewController: UIViewController {


    var delegate:Mydelegate?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        goToCart()
    }

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

        }else {
            print(delegate)
        }
    }
}

ViewController2.swift

代码语言:javascript
复制
import UIKit

class ViewController2: UIViewController, Mydelegate {


    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

    }

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

    @IBAction func goButtonAction(sender: AnyObject) {

        let vc: ViewController = storyboard?.instantiateViewControllerWithIdentifier("viewC") as! ViewController
        vc.delegate = self
        print( (vc.delegate))
        self.navigationController?.pushViewController(vc, animated: true)
    }
}

ViewController的StoryBoardId是"viewC“

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39484506

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档