XIB 的内容未加载
Contents of XIB not loading
在我的应用程序中,我有一个自定义的 UIViewController,它有一个对应的 XIB 文件。我在 XIB 文件中创建了几个按钮和标签,并创建了自定义 UIViewcontroller 的出口。然后我通过通常的方式将这个视图控制器推送到导航堆栈上:
let challengeDetails = ChallengeDetails()
self.navigationController!.pushViewController(challengeDetails, animated: true)
我知道视图控制器正在被压入堆栈,因为我将其视图的背景颜色设置为红色,而我得到的是一个空白的红色视图。我不明白的是为什么我没有看到我在 XIB 文件中创建的标签和按钮。
我检查了一系列常见的事情:文件的所有者设置为我的自定义 class。 xib 的视图连接到视图控制器(在 Outlets 中)。
当我创建项目时,我选择了 'Tabbed application',它附带了一个故事板。但是,当我创建 XIB 文件时,只需在创建新视图控制器 subclass 时选中复选框即可。 XIB 是否必须连接到故事板才能工作?
如有任何帮助,我们将不胜感激。如果有帮助,这里是自定义 class:
import UIKit
class ChallengeDetails: UIViewController {
// Outlets from xib file.
@IBOutlet weak var challengeDescriptionTextView: UITextView!
@IBOutlet weak var challengeTitleLabel: UILabel!
@IBAction func browseTitlesButton(sender: UIButton) {
browseTitlesInChallenge()
}
@IBAction func acceptChallengeButton(sender: UIButton) {
acceptThisChallenge()
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.redColor()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func browseTitlesInChallenge() {
}
func acceptThisChallenge() {
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
这是我通常做的事情:
let yourXIB = NSBundle.mainBundle().loadNibNamed(
"yourXIBName",
owner: nil,
options: nil)[0] as! yourXIBClass
view.addSubview(yourXIB)
我还将我所有的 XIB 的@IBOutlets 保存在我的 XIB 的 class 中。
问题是您没有从 nib 加载视图控制器,您只是使用空的初始化程序 ChallengeDetails()
对其进行初始化。要使用您创建的 nib 文件创建视图控制器,请使用:
let challengeDetails = ChallengeDetails(nibName: "YourNibNameHere", bundle: NSBundle.mainBundle())
确保在 Inspector 窗格中也将 Interface Builder 中笔尖的 class 设置为 ChallengeDetails
。
您应该在初始化 ViewController 时加载您的 XIB 文件。您可以在 Swift 4.2
中执行类似下面的操作
let detailsVC = ChallengeDetails(nibName :"DetailScreen", bundle : Bundle.main)
请注意上面的 DetailScreen
字符串是您的 XIB
文件的名称。
在我的应用程序中,我有一个自定义的 UIViewController,它有一个对应的 XIB 文件。我在 XIB 文件中创建了几个按钮和标签,并创建了自定义 UIViewcontroller 的出口。然后我通过通常的方式将这个视图控制器推送到导航堆栈上:
let challengeDetails = ChallengeDetails()
self.navigationController!.pushViewController(challengeDetails, animated: true)
我知道视图控制器正在被压入堆栈,因为我将其视图的背景颜色设置为红色,而我得到的是一个空白的红色视图。我不明白的是为什么我没有看到我在 XIB 文件中创建的标签和按钮。
我检查了一系列常见的事情:文件的所有者设置为我的自定义 class。 xib 的视图连接到视图控制器(在 Outlets 中)。
当我创建项目时,我选择了 'Tabbed application',它附带了一个故事板。但是,当我创建 XIB 文件时,只需在创建新视图控制器 subclass 时选中复选框即可。 XIB 是否必须连接到故事板才能工作?
如有任何帮助,我们将不胜感激。如果有帮助,这里是自定义 class:
import UIKit
class ChallengeDetails: UIViewController {
// Outlets from xib file.
@IBOutlet weak var challengeDescriptionTextView: UITextView!
@IBOutlet weak var challengeTitleLabel: UILabel!
@IBAction func browseTitlesButton(sender: UIButton) {
browseTitlesInChallenge()
}
@IBAction func acceptChallengeButton(sender: UIButton) {
acceptThisChallenge()
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.redColor()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func browseTitlesInChallenge() {
}
func acceptThisChallenge() {
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
这是我通常做的事情:
let yourXIB = NSBundle.mainBundle().loadNibNamed(
"yourXIBName",
owner: nil,
options: nil)[0] as! yourXIBClass
view.addSubview(yourXIB)
我还将我所有的 XIB 的@IBOutlets 保存在我的 XIB 的 class 中。
问题是您没有从 nib 加载视图控制器,您只是使用空的初始化程序 ChallengeDetails()
对其进行初始化。要使用您创建的 nib 文件创建视图控制器,请使用:
let challengeDetails = ChallengeDetails(nibName: "YourNibNameHere", bundle: NSBundle.mainBundle())
确保在 Inspector 窗格中也将 Interface Builder 中笔尖的 class 设置为 ChallengeDetails
。
您应该在初始化 ViewController 时加载您的 XIB 文件。您可以在 Swift 4.2
let detailsVC = ChallengeDetails(nibName :"DetailScreen", bundle : Bundle.main)
请注意上面的 DetailScreen
字符串是您的 XIB
文件的名称。