Swift - 如何从故事板中的视图控制器加载 xib 文件?
Swift - how to load a xib file from a view controller in the story board?
我有一个 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)
}
...
...
我的朋友制作了一个故事板,其中包含很多 Segues 和 ViewControllers。
当从故事板上的 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....
但我不知道如何从情节提要中的不同视图控制器或 ViewController 中的按钮按下 (IBAction) 加载该 xib 文件。
您加载视图控制器及其关联的 nib 文件的方式与您在代码中的做法完全相同:
let mainViewController = MainViewController(nibName: "MainViewController", bundle: nil)
这会实例化视图控制器,当您呈现或推送到它时,视图将从 nib 加载并放置到界面中。
您不能直接从情节提要中转到 MainViewController
。但是您可以加载并显示它,就像您执行应用委托一样。
你想如何显示MainViewController
?将它推到导航堆栈上,以模态方式呈现它,还有别的吗?我假设你正在使用 UINavigationController
并且你想将它压入堆栈。
在你朋友的 UIViewController
class 中,添加一个方法来加载和呈现你的 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
}
解决了...它就在我眼前,我想我不得不问问题自己写答案,哈哈,我很聪明,我把这段代码添加到视图控制器故事板,它就像一个魅力。
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 视图控制器中我添加了一个 return 动作
playBtn.addTarget(self, action: #selector(goBackToMainVC), forControlEvents: .TouchUpInside)
self.delegate?.backFromM()
谢谢大家!!
作为 class 变量赋值:
var nibName: NibViewClass?
分配给笔尖对应的class。
在视图控制器中任何你想加载 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 分配给对应的 class 后的任何时候,您都可以从 viewController 内部分配其局部变量。如果愿意,您还可以选择使用 x 位置为笔尖设置动画。我会使用相同的函数,将 y 值反转,以便在完成后将其发送回屏幕外。
我有一个 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)
}
...
...
我的朋友制作了一个故事板,其中包含很多 Segues 和 ViewControllers。
当从故事板上的 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....
但我不知道如何从情节提要中的不同视图控制器或 ViewController 中的按钮按下 (IBAction) 加载该 xib 文件。
您加载视图控制器及其关联的 nib 文件的方式与您在代码中的做法完全相同:
let mainViewController = MainViewController(nibName: "MainViewController", bundle: nil)
这会实例化视图控制器,当您呈现或推送到它时,视图将从 nib 加载并放置到界面中。
您不能直接从情节提要中转到 MainViewController
。但是您可以加载并显示它,就像您执行应用委托一样。
你想如何显示MainViewController
?将它推到导航堆栈上,以模态方式呈现它,还有别的吗?我假设你正在使用 UINavigationController
并且你想将它压入堆栈。
在你朋友的 UIViewController
class 中,添加一个方法来加载和呈现你的 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
}
解决了...它就在我眼前,我想我不得不问问题自己写答案,哈哈,我很聪明,我把这段代码添加到视图控制器故事板,它就像一个魅力。
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 视图控制器中我添加了一个 return 动作
playBtn.addTarget(self, action: #selector(goBackToMainVC), forControlEvents: .TouchUpInside)
self.delegate?.backFromM()
谢谢大家!!
作为 class 变量赋值:
var nibName: NibViewClass?
分配给笔尖对应的class。
在视图控制器中任何你想加载 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 分配给对应的 class 后的任何时候,您都可以从 viewController 内部分配其局部变量。如果愿意,您还可以选择使用 x 位置为笔尖设置动画。我会使用相同的函数,将 y 值反转,以便在完成后将其发送回屏幕外。