简体   繁体   English

我的协议没有调用

[英]My Protocol is not Calling

I am using protocol for call method but my method does not call.Is there is any example which i use.我正在使用调用方法的协议,但我的方法不调用。是否有任何我使用的示例。

Here is my code:这是我的代码:

ViewController视图控制器

    class ViewController: UIViewController {
      override func viewDidLoad() {
        super.viewDidLoad()
                // Do any additional setup after loading the view.
    }


extension ViewController :ViewController1Delegate
{
    func hello()
    {
        println("hbgyguyg");
    }
}

In View Controller 1在视图控制器 1

import UIKit
@objc
protocol ViewController1Delegate
{
   optional func hello()
}
class ViewController1: UIViewController {
 var delegate: ViewController?
    override func viewDidLoad() {
        super.viewDidLoad()
                    delegate?.hello()
             }

}

Please Help, I am new in Swift.Any help would be apperciated.请帮助,我是 Swift 的新手。任何帮助都会得到帮助。 Thanks in Advance提前致谢

An example demo.一个示例演示。

ViewController file视图控制器文件

import UIKit

class ViewController: UIViewController, PopUpViewControllerDelegate
{
    var popupVC: PopUpViewController!;

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

        self.view!.backgroundColor = UIColor.whiteColor();

        self.popupVC = PopUpViewController();
        self.popupVC.delegate = self;


        self.showPopUpVC();
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    func popUpViewControllerDidPressOK(popUpVC: PopUpViewController) {

        println("Yay?");

        self.closePopUpVC();
    }

    func showPopUpVC()
    {
        let delayTime = dispatch_time(DISPATCH_TIME_NOW,
            Int64(1.0 * Double(NSEC_PER_SEC)))

        dispatch_after(delayTime, dispatch_get_main_queue()) {

            self .presentViewController(self.popupVC, animated: true, completion: nil);

        }
    }

    func closePopUpVC()
    {
        let delayTime = dispatch_time(DISPATCH_TIME_NOW,
            Int64(1.0 * Double(NSEC_PER_SEC)))

        dispatch_after(delayTime, dispatch_get_main_queue()) {

            self.dismissViewControllerAnimated(true, completion: nil);

        }
    }
}

PopUpViewController file弹出视图控制器文件

import UIKit

protocol PopUpViewControllerDelegate
{
    func popUpViewControllerDidPressOK(popUpVC: PopUpViewController);
}

class PopUpViewController: UIViewController {

    var delegate: PopUpViewControllerDelegate!;

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view!.backgroundColor = UIColor.redColor();

        // Do any additional setup after loading the view.

        self.delegate!.popUpViewControllerDidPressOK(self);
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

Notice how in my ViewController viewDidLoad() method, I have a line that initialises the popUp view controller and then set its delegate to be the ViewController itself:注意在我的ViewController viewDidLoad()方法中,我有一行初始化 popUp 视图控制器,然后将其委托设置为ViewController本身:

self.popupVC = PopUpViewController();
self.popupVC.delegate = self; // you're missing this line I believe ?

I don't use Interface Builder or Storyboard but maybe select your VC1 in your Storyboard and look in the connections inspector to see if you drag a line from "delegate" to your ViewController file owner thing.我不使用 Interface Builder 或 Storyboard,但可能在 Storyboard 中选择您的 VC1 并查看连接检查器,看看您是否将一行从“委托”拖到您的ViewController文件所有者。

In the end, you should see a red screen popup after 1 second, followed by the word "Yay?"最后,您应该会在 1 秒后看到一个红屏弹出,然后是“Yay?”这个词。 logged into your Xcode console and finally the popupVC dismisses.登录到您的 Xcode 控制台,最后 popupVC 关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM