Ios 在UIBarItem点击时未调用委托
点击Ios 在UIBarItem点击时未调用委托,ios,iphone,swift,delegates,Ios,Iphone,Swift,Delegates,点击UIBarItem,不调用委托方法。我接受了很多建议,都和我写的一样 我根据建议试过了,没有解决办法。无论什么地方,都给代表打电话。我做了每一件事 class ViewController: CommonClass,Mydelegate { var vc = CommonClass() override func viewDidLoad() { super.viewDidLoad() vc = CommonClass(nibName: "C
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)
}
}
}
我看到的是:
CommonClass
继承UIViewController
ViewController
继承CommonClass
CommonClass
方法ViewController
直接调用goToCart()
方法CommonClass
的方法,我看到的是:
CommonClass
继承UIViewController
ViewController
继承CommonClass
CommonClass
方法ViewController
直接调用goToCart()
方法因此,不需要使用委托模式来调用
CommonClass
的方法—一个示例来演示如何使用自定义委托:
ViewController.swift
ViewController2.swift
ViewController的故事板ID是“viewC”一个使用自定义代理演示的示例: ViewController.swift ViewController2.swift
ViewController的故事板ID为“viewC”触摸时是否打印“hi”消息?是,但如果条件失败,下一次打印为“nil”?MyDelegate的外观如何?@ShadowOf是。自定义代理,只打印hello(不打印)。触摸时是否打印“hi”消息?是,但如果条件失败,下一次打印为“nil”?您的MyDelegate看起来如何?@ShadowOf是。自定义委托,只打印hello(不打印)。如果遗漏了有关应用程序工作逻辑的内容,请告诉我。如果遗漏了有关应用程序工作逻辑的内容,请告诉我。
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)
}
}
}
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)
}
}