我在嘗試從父視圖控制器訪問我的.xib文件中的元素時遇到問題。我的代碼如下:無法在父文件中設置.xib文件中的標籤文本 - Swift
GraphScrollViewController.swift
class GraphScrollViewController: UIViewController {
var graphScrollMenu: CAPSPageMenu?
var controllerArray: [UIViewController] = []
override func viewDidLoad() {
super.viewDidLoad()
var weekViewController : RecentWeekGraphViewController = RecentWeekGraphViewController(nibName: "RecentRulesWeekGraphViewController", bundle: nil)
weekViewController.title = "Week"
controllerArray.append(weekViewController)
var monthViewController : RecentMonthGraphViewController = RecentMonthGraphViewController(nibName: "RecentMonthGraphViewController", bundle: nil)
monthViewController.title = "Month"
controllerArray.append(monthViewController)
var parameters: [CAPSPageMenuOption] = [
.ScrollMenuBackgroundColor(UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 0)),
.ViewBackgroundColor(UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 0)),
.SelectionIndicatorColor(UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1)),
.BottomMenuHairlineColor(UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 0)),
.UnselectedMenuItemLabelColor(UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 0.5)),
.MenuItemFont(UIFont(name: "Avenir-Book", size: 15.0)!),
.MenuHeight(40.0),
.MenuItemWidth(90.0),
.CenterMenuItems(true)
]
graphScrollMenu = CAPSPageMenu(viewControllers: controllerArray, frame: CGRectMake(0.0, 0.0, self.view.frame.width, self.view.frame.height), pageMenuOptions: parameters)
self.view.addSubview(graphScrollMenu!.view)
}
}
RecentWeekGraphViewController.swift
class RecentWeekGraphViewController: UIViewController {
@IBOutlet weak var xibTestLabel: UILabel!
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
當我嘗試使用設置從GraphScrollView.swift的xibTestLabel文本weekViewController.xibTestLabel.text =「一些文字「我得到:
fatal error: unexpectedly found nil while unwrapping an Optional
我的網點全部似乎連接正確。
由於
你正在使用哪個iOS SDK/Xcode來構建應用程序? –
你是否與monthViewController有同樣的問題? –
你究竟在哪裏試圖訪問'GraphScrollViewController'中的'xibTestLabel'? – hennes