我正在使用表视图控制器实现自定义水平多段,作为每个段的内容。但是如何为所有段仅实现一个控制器,因为所有段的视图都相同,但只有数据发生变化。 我想将段的当前索引传递给表视图控制器以获取特定数据。 我使用的是来自 - https://codeload.github.com/uacaps/PageMenu/zip/master的CAPSPageMenu.swift文件?
这是我的代码 -
class Entrance_main_controller: UIViewController {
var pageMenu : CAPSPageMenu?
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
// MARK: - UI Setup
self.title = "Entrance Exam"
// Initialize view controllers to display and place in array
var controllerArray : [UIViewController] = []
let controller1 : Entrance_Content_ViewController = Entrance_Content_ViewController(nibName: "TestTableViewController", bundle: nil)
controller1.title = "Engineering"
controllerArray.append(controller1)
let controller2 : Entrance_Content_ViewController = Entrance_Content_ViewController(nibName: "TestTableViewController", bundle: nil)
controller2.title = "Medical"
controllerArray.append(controller2)
let controller3 : Entrance_Content_ViewController = Entrance_Content_ViewController(nibName: "TestTableViewController", bundle: nil)
controller3.title = "Management"
controllerArray.append(controller3)
// Customize menu (Optional)
let parameters: [CAPSPageMenuOption] = [
.ScrollMenuBackgroundColor(UIColor(red: 30.0/255.0, green: 30.0/255.0, blue: 30.0/255.0, alpha: 1.0)),
.ViewBackgroundColor(UIColor(red: 20.0/255.0, green: 20.0/255.0, blue: 20.0/255.0, alpha: 1.0)),
.SelectionIndicatorColor(UIColor.orangeColor()),
.BottomMenuHairlineColor(UIColor(red: 70.0/255.0, green: 70.0/255.0, blue: 80.0/255.0, alpha: 1.0)),
.MenuItemFont(UIFont(name: "HelveticaNeue", size: 13.0)!),
.MenuHeight(40.0),
.MenuItemWidth(90.0),
.CenterMenuItems(true)
]
// Initialize scroll menu
pageMenu = CAPSPageMenu(viewControllers: controllerArray, frame: CGRectMake(0.0, 0.0, self.view.frame.width, self.view.frame.height), pageMenuOptions: parameters)
self.addChildViewController(pageMenu!)
self.main_content_view.addSubview(pageMenu!.view)
pageMenu!.didMoveToParentViewController(self)
}
//here I am getting current index
func get_current_page() -> Int {
let currentIndex = pageMenu!.currentPageIndex
return currentIndex;
}
// MARK: - Container View Controller
override func shouldAutomaticallyForwardAppearanceMethods() -> Bool {
return true
}
override func shouldAutomaticallyForwardRotationMethods() -> Bool {
return true
}
}
这里我正在为所有段使用Entrance_Content_ViewController类。在这个控制器中,我为当前页面索引调用 get_current_page()方法。
class Entrance_Content_ViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
let entrance_main_controller = Entrance_main_controller()
let page = entrance_main_controller.get_current_page()
print("page no\(page)")
self.tableView.registerNib(UINib(nibName: "EntranceTableViewCell", bundle: nil), forCellReuseIdentifier: "EntranceTableViewCell")
}
但我收到此错误 - 致命错误:在解包可选值(lldb)时意外发现nil 请帮我解决这个问题。 谢谢