2017-07-03 46 views
1

我试图使用自定义的协议,但它的委托方法不会被调用自定义协议的委托方法不是要求

ViewController3代码从另一个视图控制器改变一个视图控制器的图标:

当我点击关闭按钮它的委托方法不会在我的ViewController2中被调用。

protocol ViewController3Delegate: class { 

    func changeLable(_ text: String) 
} 

class ViewController3: UIViewController { 

    weak var delegate: ViewController3Delegate? 

    override func viewDidLoad() { 
    super.viewDidLoad() 

    } 

    override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 

    } 

    @IBAction func btnCloseAction(_ sender: Any) { 

    delegate?.changeLable("fillter applied") 
    self.dismiss(animated: true, completion: nil) 
    } 
} 

ViewController2代码:

class ViewController2: UIViewController,ViewController3Delegate { 

    @IBOutlet weak var lblReport: UILabel! 

    let VC3 = ViewController3(nibName: "ViewController3", bundle: nil) 

    override func viewDidLoad() { 

    super.viewDidLoad() 
    VC3.delegate = self 
    } 

    func changeLable(_ text: String) { 

    print("delegate called") 
    } 

    override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 

    } 
} 

任何人都知道在哪里我错了,请建议我一些解决方案

+0

来定义segueidentifer名称作为不添加'VC3.view'作为子视图,我怀疑你有两个'ViewController3'实例。作为你在'ViewController2'中的属性(你没有看到)以及我们没有看到代码的属性。也许由Storyboard实例化。 'ViewController2's'btnCloseAction'被调用,但它的'delegate'没有被设置。 – shallowThought

+0

@shallowThought什么是我尝试了很多替代方案,但它不工作的解决方案。 –

+0

你了解我的嫌疑人吗? – shallowThought

回答

0

你应该首先目前ViewController3从ViewController2这样的:

let VC3 = ViewController3(nibName: "ViewController3", bundle: nil) 
VC3.delegate = self 
self.presentViewController(VC3, animated: true, completion: nil) 

另外,您可以删除这条线以上的viewDidLoad

let VC3 = ViewController3(nibName: "ViewController3", bundle: nil) 
0

您还没有定义在你的ViewController2一个委托,它将被用来在ViewController3委托。

看到这一点:

protocol ViewController3Delegate: class { 

     func changeLable(_ text: String) 
    } 

    class ViewController3: UIViewController { 

     weak var delegate: ViewController3Delegate? 

     override func viewDidLoad() { 
     super.viewDidLoad() 

     } 

     override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 

     } 

     @IBAction func btnCloseAction(_ sender: Any) { 
     if delgate !=nil { 

     delegate?.changeLable("fillter applied") 
     self.dismiss(animated: true, completion: nil) 
    } 
     } 
    } 

,然后在ViewController2类:

class ViewController2: UIViewController,ViewController3Delegate { 

     @IBOutlet weak var lblReport: UILabel! 


     override func viewDidLoad() { 

     super.viewDidLoad() 
     } 

     func changeLable(_ text: String) { 

     print("delegate called") 
     } 

     override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 

     } 
      override func prepare(for segue: UIStoryboardSegue, sender: Any?) 
      { 

       if segue.identifier = "Yoursegueientifier" 
        { 
        let vc = segue.destination as! ViewController3 
        vc.delegate = self 
        }     
      } 
    } 

注意:你在你的故事板

+0

感谢您的建议,但与我的情况下不同,我有3个标签的TabBar控制器,所以它不工作,如果你知道如何与代表与Tabbar的工作,请做建议我一些解决方案 –

+0

所以你想在标签之间传递数据? –

+0

是的,它像应用过滤器,我有三个选项卡的tabbar控制器,第一个和第三个选项卡包含的数据可以在选项卡2上应用选项卡上的过滤器2一个视图显示过滤器选项基于过滤器数据应该得到过滤它可以是第一次查看或第三次查看它并不重要,我已经尝试与委托在tabbar控制器上工作,它工作我不知道它不与其他控制器工作的原因 –