我有自定义uinavigation类。在声明的自定义委托方法中。如何访问该方法以查看控制器

时间:2016-09-13 09:15:39

标签: swift

@objc  protocol MyDelegate {
    func buttonAction()
}

class CustomNavigationBar: UINavigationController {

var delegte : MyDelegate?

override func viewDidLoad() {
    super.viewDidLoad()
    let button = UIButton.init(frame: CGRectMake(200, 10, 50, 30))
    button.setBackgroundImage(UIImage(named: "a.png"), forState: .Normal)
    button.addTarget(self, action: "testing", forControlEvents: .TouchUpInside)
    self.navigationBar.addSubview(button)
}

func testing(){
    self.delegte?.buttonAction()
    print("Pavan")
}

如果我按下此按钮,则测试正在呼叫。

但是在viewcontroller中调用委托方法却给出了错误

class ViewController: UIViewController,MyDelegate{

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.title = "hi"
    let vc = CustomNavigationBar()
    vc.delegte = self
}

func buttonAction() {
    print("Tupale")
}

1 个答案:

答案 0 :(得分:0)

你会介意提示错误信息吗?

更新: 在CustomNavigationBar课程中,您必须将var delegte : MyDelegate?更改为var delegte : UIViewController?

然后在ViewController类中,您可以将self作为UIViewController的实例设置为vc的委托。

试一试。