Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在UIBarItem点击时未调用委托_Ios_Iphone_Swift_Delegates - Fatal编程技术网

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)
        }
    }