【问题标题】:How to push user to ViewController from non UIView class [duplicate]如何将用户从非 UIView 类推送到 ViewController [重复]
【发布时间】:2017-08-09 05:46:52
【问题描述】:

我想知道如何将用户从常规的 swift 类推回特定的 ViewController 而不是非 UIView 类

例子

class nonUI {
 function Push() {
  //How to send user back to specific VC here?
 }
}

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    这是一个通用方法,如果需要,您可以在类内或类外使用推送,否则如果视图控制器的实例在堆栈中,它将弹出:

    func pushIfRequired(className:AnyClass) {
    
        if (UIViewController.self != className) {
            print("Your pushed class must be child of UIViewController")
            return
        }
        let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        var isPopDone = false
        let mainNavigation = UIApplication.shared.delegate?.window??.rootViewController as? UINavigationController
        let viewControllers = mainNavigation!.viewControllers
        for vc in viewControllers {
            if (type(of: vc) == className) {
                mainNavigation?.popToViewController(vc, animated: true)
                isPopDone =  true
                break
            }
        }
        if isPopDone == false{
            let instanceSignUp = storyboard.instantiateViewController(withIdentifier: NSStringFromClass(className)) // Identifier must be same name as class
            mainNavigation?.pushViewController(instanceSignUp, animated: true)
        }
    }
    

    用途

    pushIfRequired(className: SignupVC.self)
    

    【讨论】:

      【解决方案2】:

      您还可以利用NotificationCenter 来实现“请求视图控制器”的松耦合方式;如果你愿意的话。

      例如,创建一个自定义UINavigationController 来观察自定义通知,并在收到通知后查找请求的UIViewController 并弹回它。

      class MyNavigationController : UINavigationController {
      
          override func viewDidLoad() {
              super.viewDidLoad()
      
              NotificationCenter.default.addObserver(forName: NSNotification.Name("RequestViewController"), object: nil, queue: OperationQueue.main) { [unowned self] (note) in
                  guard let targetType = note.object as? UIViewController.Type else {
                      print("Please provide the type of the VC to display as an `object` for the notification")
                      return
                  }
      
                  // Find the first VC of the requested type
                  if let targetVC = self.viewControllers.first(where: { $0.isMember(of: targetType) }) {
                      self.popToViewController(targetVC, animated: true)
                  }
                  else {
                      // do what needs to be done? Maybe instantiate a new object and push it?
                  }
              }
          }
      }
      

      然后在你想要返回到特定 ViewController 的对象中,发布通知。

      @IBAction func showViewController(_ sender: Any) {
          NotificationCenter.default.post(Notification(name: NSNotification.Name("RequestViewController"), object: ViewController2.self))
      }
      

      现在,将这种方法用于其他演示样式也相当容易。 除了使用 NotificationCenter,您还可以召集一个 Mediator 来实现松散耦合。

      【讨论】:

      • 它并没有完全那样工作,但你的建议是最好的。很简单,我添加了通知并传递给 UI 类中的函数,然后从那里执行事务,并从非 UI 类调用该通知。感谢您的想法!
      【解决方案3】:

      你不能。 UIViewController 及其子类只能处理屏幕之间的导航。

      在您的情况下,需要将链接(变量)传递给自定义类中的导航控制器。

      喜欢:

      class nonUI {
          var navigationController: UINavigationController?
      
          init(navigationController: UINavigationController) {
              self.navigationController = navigationController
          }
      
       function Push() {
        //How to send user back to specific VC here?
        navigationController?.popViewController(animated: true)
       }
      }
      

      【讨论】:

      • 但是如何将 navigationController 传递给非 UI 类?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多