【问题标题】:How to push and present to UIViewController programmatically From a XIB in iOS Swift 3如何以编程方式从 iOS Swift 3 中的 XIB 推送和呈现给 UIViewController
【发布时间】:2017-05-12 08:54:59
【问题描述】:

我正在 swift 3.0 中开发一个项目,我使用以下机制从一个 UIViewController 导航到另一个。

if let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MP3ViewController") as? MP3ViewController {
  viewController.mediaDetails = mp3Details
  if let navigator = navigationController {
      navigator.pushViewController(viewController, animated: true)
  }
}

但是,当我在 XIB 文件中尝试相同的机制时,它给了我一个错误

错误:说“使用未解析的标识符navigationController”

我怎样才能在这里创建一个 navigationController 实例? 下面我展示了我的 XIB 类的代码,它给了我一个错误

if let categoriesController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "CategoriesViewController") as? CategoriesViewController {
  categoriesController.categoryName = trimmedCategortyNameText

  if let navigator = navigationController {
      navigator.pushViewController(categoriesController, animated: true)
  }
}

【问题讨论】:

  • 请展示创建navigationController的代码。
  • 你需要调用 self.navigationController.pushViewController 我猜。并确保您当前的视图控制器嵌入在 NavigationController 中。
  • 问题是XIB的类类型是UIView。 bcz 你不能声明为“self.navigationController”。我将如何解决这个问题
  • @danu 看到我的回答你可以把代码放在你的 xib 类文件中

标签: ios swift swift3 uinavigationcontroller


【解决方案1】:

希望对你有帮助

    let sampleVC = SampleViewController(nibName: "SampleViewController", bundle: nil)
    self.navigationController?.pushViewController(sampleVC, animated: true)

编辑 根据您的要求,您需要从 UIView 导航到 UIViewController。 您可以通过两种方式实现这一目标

  • 使用委托/回调
  • 使用NavigationController的引用

我将向您展示第二种方式。

在AppDelegate中取一个变量UINavigationController

AppDelegate

var navController : UINavigationController?

然后将导航控制器分配给此变量,从您导航到 UIView 的位置。 (假设从 SampleViewController 中添加 UIView,然后编写)

ViewController 中(这是我的 MainVC)

    let appDelegate = UIApplication.shared.delegate! as! AppDelegate
    appDelegate.navController = self.navigationController

然后在 UIView 类中(SampleView 在我的例子中)

    let appDelegate = UIApplication.shared.delegate! as! AppDelegate

    let sampleVC = SampleViewController(nibName: "SampleViewController", bundle: nil)

    appDelegate.navController?.pushViewController(sampleVC, animated: true)

我验证了这一点。 快乐编码..

【讨论】:

  • 您能否将其修改为我的问题并发布答案
  • @danu 我认为您需要将 UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MP3ViewController") 替换为? MP3ViewController with MP3ViewController(nibName: "SampleViewController", bundle: nil) 让我知道这是否适合你
  • 问题是XIB的类类型是UIView。 bcz 你不能声明为“self.navigationController”。我将如何解决这个问题
  • 我认为您不能从 ViewController 对 UIView 类执行推送或弹出操作。如果您想从 UIView 推送到 viewController,那么您可以参考 AppDelegate 中的 navigationController
  • @danu 如果你想从任何 ViewController 导航到 UIView,那么你可以在 viewControllers 视图上添加视图,并添加动画来制作推送动画的效果。让我知道这是否有帮助。
【解决方案2】:

你可以这样做:

如果您使用的是故事板

if let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MP3ViewController") as? MP3ViewController {
        viewController.mediaDetails = mp3Details
        if let navigator = self.navigationController {
            navigator.pushViewController(viewController, animated: true)
        }else{
            let navigation  = UINavigationController(rootViewController:viewController)
            self.present(navigation, animated: true, completion: nil)
        }
}

使用 Xib

像这样初始化一个 UIViewController 的实例:

   let  vc =  MP3ViewController(nibName:"MP3ViewController",bundle:Bundle.main)
   vc.mediaDetails = mp3Details
    if let navigator = self.navigationController {
        navigator.pushViewController(viewController, animated: true)
    }else{
        let navigation  = UINavigationController(rootViewController:viewController)
        self.present(navigation, animated: true, completion: nil)
    }

【讨论】:

  • 你看到了吗mechanism in the XIB file
  • 不,我不知道这个。它说'类型的值......View没有成员navigationController
  • 我已编辑答案以使用 xib @Anbu.Karthik
  • 问题是XIB的类类型是UIView。 bcz 你不能声明为“self.navigationController”。我将如何解决这个问题
【解决方案3】:

您可以通过 Appdelegate 使用与 Xib 相同的机制

let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let currentOrderVC = storyboard.instantiateViewController(withIdentifier: "yourVcId") as! YourVC

        var nav1 = UINavigationController()
        nav1.viewControllers = [currentOrderVC]

        UIApplication.shared.keyWindow?.rootViewController
        UIApplication.shared.keyWindow?.rootViewController = nav1
        UIApplication.shared.keyWindow?.makeKeyAndVisible();

【讨论】:

    猜你喜欢
    • 2017-02-17
    • 2015-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多