委派方法不调用点击UIBarItem。我经历了这么多的建议,都是我写的一样的。
我根据建议尝试了,没有解决方案。在给定的任何地方调用委托。我做了每一件事。
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)
}
}
}发布于 2016-09-14 15:28:09
我看到的是:
CommonClass继承UIViewController.ViewController继承CommonClass.ViewController.中的CommonClass方法。
goToCart()方法因此,不需要使用委托模式来调用CommonClass的方法。
发布于 2016-09-14 16:59:21
演示使用自定义委托的示例示例:
ViewController.swift
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
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“
https://stackoverflow.com/questions/39484506
复制相似问题