【问题标题】:Swift - how to load a xib file from a view controller in the story board?Swift - 如何从故事板中的视图控制器加载 xib 文件?
【发布时间】:2016-11-26 23:49:44
【问题描述】:

我有一个 MainViewController.swift,它连接到一个 Xib 文件(称为 MainViewController)。 MainViewController 连接到 MenuViewController.swift 及其 Xib(称为 MenuViewController)。 MainViewController 使用以下代码调用 MenuViewContoller,

override func viewDidLoad() {
    super.viewDidLoad()
    ...
    menuBtn.addTarget(self, action: #selector(callMenuModal), forControlEvents: .TouchUpInside)
    ....

}
func callMenuModal() {
    let menuVC = MenuViewController()
    menuVC.delegate = self
    menuVC.modalPresentationStyle = .OverCurrentContext
    presentViewController(menuVC, animated: false, completion: nil)
}
.......
func backFromMenu() {
    self.dismissViewControllerAnimated(true, completion: nil)
    }
...
...

我的朋友们制作了一个包含很多 Segue 和 ViewController 的故事板。

当从故事板上的 ViewController 按下按钮时,我现在必须加载我的 .xib 文件及其对应的视图控制器。我一直在寻找解决方案,但找不到。

以前我直接从 AppDelegate 调用我的 xib,所以我不必做任何事情。我在故事板中只有一个视图控制器和整个应用程序的应用程序委托,我在委托中使用的代码如下所示,

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    window = UIWindow(frame: UIScreen.mainScreen().bounds)

    let mainViewController = MainViewController(nibName: "MainViewController", bundle: nil)

    window?.rootViewController = mainViewController
    window?.makeKeyAndVisible()
    return true        
}
........rest of app delegate....

但我不知道如何从情节提要中的不同视图控制器或视图控制器中的按钮按下 (IBAction) 加载该 xib 文件。

【问题讨论】:

标签: swift uikit uistoryboard xib


【解决方案1】:

您不能直接从情节提要中转到您的MainViewController。但是您可以加载并显示它,就像您执行应用委托一样。

你想如何显示MainViewController?将其推送到导航堆栈上,以模态方式呈现,还是其他?我将假设您正在使用 UINavigationController 并且您想将其推送到堆栈中。

在你朋友的UIViewController 类中,添加一个方法来加载和展示你的MainViewController

func presentMainViewController() {
    let mainVC = MainViewController(nibName:"MainViewController", bundle:nil)
    self.navigationController.pushViewController(mainVC, true);
    // you could present it another way, such as:
    // self.presentViewController(mainVC, true, nil)
    // to present it as a modal
}

【讨论】:

    【解决方案2】:

    作为类变量赋值:

    var nibName: NibViewClass?
    

    将其分配给为笔尖制作的相应类。

    在视图控制器中,无论您想加载 nib 的任何位置,都可以调用

    nibName = NSBundle.mainBundle().loadNibNamed("NibView", owner: self, options: nil) as? NibViewClass
    

    确保“NibView”== Nib UI 文件的名称。然后你需要像这样将它添加为子视图:

    if nibName != nil {
        self.view.addsubview(nibName!)
    
        //add this line to make it appear where you want it.
        nibName.frame = CGRectMake(xPosition, yPositionOffScreen, nibWidth, nibHeight)
    }
    

    现在这将为您完成大部分工作,但我建议您也为它制作动画。有几种不同的方法可以做到这一点,但我经常使用的一种是这样的:

    UIView.animateWithDuration(0.7, delay: 1.0, options: .CurveEaseIn, animations: {
            nibName.frame.origin.y = yPositionOnScreen
      }, completion: { finished in
            //code to run after nib finishes movement
      })
    

    在将 nibName 分配给它的相应类之后的任何时候,您都可以从 viewController 内部分配其局部变量。如果愿意,您还可以选择在 x 位置为笔尖设置动画。完成后,我将使用相同的函数,将 y 值反转以将其发送回屏幕外。

    【讨论】:

      【解决方案3】:

      解决了...就在我眼前,猜想我必须自己问问题才能写答案,哈哈,我太聪明了,我从故事板将这段代码添加到视图控制器中它就像一个魅力。

      import UIKit
      
      class ViewController: UIViewController, communicationControllerM {
      
          @IBOutlet weak var Btn: UIButton!
      
          override func viewDidLoad() {
              super.viewDidLoad()
      
              Btn.addTarget(self, action: #selector(callMenuModal), forControlEvents: .TouchUpInside)
          }
      
          func callMenuModal() {
              let mainVC = MainViewController()
              mainVC.delegate = self
              mainVC.modalPresentationStyle = .OverCurrentContext
              presentViewController(mainVC, animated: false, completion: nil)
          }
      
          override func didReceiveMemoryWarning() {
              super.didReceiveMemoryWarning()
              // Dispose of any resources that can be recreated.
          }
      
          func backFromM() {
              self.dismissViewControllerAnimated(true, completion: nil)
          }
      }
      

      在我的 xib 视图控制器中,我添加了一个返回操作

      playBtn.addTarget(self, action: #selector(goBackToMainVC), forControlEvents: .TouchUpInside)
      self.delegate?.backFromM()
      

      谢谢大家!!

      【讨论】:

      【解决方案4】:

      您完全按照您在代码中执行的方式加载视图控制器及其关联的 nib 文件:

      let mainViewController = MainViewController(nibName: "MainViewController", bundle: nil)
      

      这会实例化视图控制器,当你展示或推送到它时,视图将从 nib 加载并放置到界面中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-11
        • 2013-08-04
        • 2021-08-05
        相关资源
        最近更新 更多