Ios 从swift中的弹出视图控制器中识别当前视图控制器

Ios 从swift中的弹出视图控制器中识别当前视图控制器,ios,swift,Ios,Swift,假设我们有两个视图控制器 FirstViewController.swift PopupViewController.swift 已在FirstViewController上加载PopupViewController。现在,我如何从PopupViewController调用FirstViewController中的函数。使用本机Swift协议: protocol masterGreeting { func sayHi() } class FirstVC: UIViewControlle

假设我们有两个视图控制器

  • FirstViewController.swift
  • PopupViewController.swift

  • 已在FirstViewController上加载PopupViewController。现在,我如何从PopupViewController调用FirstViewController中的函数。

    使用本机Swift协议:

    protocol masterGreeting {
        func sayHi()
    }
    
    class FirstVC: UIViewController, masterGreeting {
          func sayHi() {
            print("Hi")
          }
    
    
        // When you present PopUpViewController
        func displaySecondVC() {
           let vc = secondController()
           vc.delegate = self
           self.present(vc, animated: true)
        }
    
    } 
    
    在PopUpViewController中:

    class PopUpViewController: UIViewController {
        var delegate: masterGreeting?
    
         self.delegate?.sayHi() // Say HI
    
    
    }
    
    有关
    协议
    的更多信息,请参见:


    另一种方法:

    class FirstViewController: UIViewController {
        @IBAction func openPopupVC(_ sender: Any) {
            if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PopUpViewContraller") as? PopUpViewContraller {
                controller.handler = {[weak self] in
                    self?.callFromHandler()
                }
                self.present(controller, animated: true, completion: nil)
            }
        }
        
        func callFromHandler() {
            print("Working..!!!")
        }
    }
    
    class PopUpViewContraller: UIViewController {
        var handler: (()->())?
        
        @IBAction func saveButton(_ sender: Any) {
            self.dismiss(animated: true) {
                self.handler?()
            }
        }
    }
    
    使用
    闭包
    使其正常工作Apple
    Swift
    中广泛使用了
    closures

  • PopupViewController
  • 当显示
    PopupViewController
    时,从
    FirstViewController
    设置
    closure
  • PopupViewController
    被解除时,调用
    closure
  • 示例:

    class FirstViewController: UIViewController {
        @IBAction func openPopupVC(_ sender: Any) {
            if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PopUpViewContraller") as? PopUpViewContraller {
                controller.handler = {[weak self] in
                    self?.callFromHandler()
                }
                self.present(controller, animated: true, completion: nil)
            }
        }
        
        func callFromHandler() {
            print("Working..!!!")
        }
    }
    
    class PopUpViewContraller: UIViewController {
        var handler: (()->())?
        
        @IBAction func saveButton(_ sender: Any) {
            self.dismiss(animated: true) {
                self.handler?()
            }
        }
    }
    

    代理当然是另一种方式。您可以采用任何一种方法。

    谢谢您回答我的问题

    最后,我得到了上述问题的答案

    我们可以使用通知中心解决此问题

    1) 在弹出视图控制器()中

    2) 在第一视图控制器中

    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "botIsRemoved"), object: self)
    
    内部视图didloard()

    并添加以下函数

    @objc func removeBotNow(notification : Notification){
    
            // your tasks
    
        }
    

    您可以通过Observer或@procol(自定义委托)实现这一点。委托在这里非常有用。请使用弱委托以确保安全。他不需要在这件事上坚持到底scenario@JoshuaFrancisRoman最后,我不知道该代码将如何使用,因此op必须进行一些更改。请注意代码中的保留周期。将
    委托
    的引用设为弱引用,因此将
    主问候语
    协议声明为:
    协议主问候语:AnyObject
    @AhmadF。欢迎捐款。请编辑一下。我不想因为你的代码而受到表扬