在使用基于MVC的Swift 1.2和Xcode 6.3开发Tabbed IOS应用程序的斗争中,我使用可视化的Storyboard元素而不是以编程方式进行,因为我不是一个经验丰富的开发人员。在下面附带的图片中,您可以在App的StoryBoard
中看到架构:
该应用程序包含:
TabBarController
,其中包含四个TabBar Items
。Item
在ViewController
中都有自己的Storyboard
。relationship seque
中的StoryBoard
(ViewControllers)相关联。ViewController
中的每个StoryBoard
都有自己的Class
。NavigationController
,因为我正在使用PageMenu项目https://github.com/uacaps/PageMenu来包含一个带有两个孩子ViewControllers
问题我到目前为止:
child ViewControllers
未与TabBar Item
中的上一个StoryBoard
相关联,如上图所示,仅在parent ViewController Class
中实例化(PageMenuViewController1 ),通常这PageMenu
有效,但有时最后TabBar Item
消失,我对此问题非常困惑。override func viewWillAppear
child ViewController
两次,我已经包含了println(“ClubsController viewWillAppear”)。ViewControllers的代码是
import UIKit
class ClubsViewController: UIViewController, UITableViewDataSource{
@IBOutlet var tableview:UITableView!
let apiClient = ApiClient()
var clubs: [Club]!
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
println("ClubsController viewWillAppear")
apiClient.clubsService.getList() { clubs, error in
if clubs != nil {
self.clubs = clubs
self.tableview?.reloadData()
}
else {
println("error: \(error)")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.clubs?.count ?? 0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("clubObjectCell") as! ClubTableViewCell
cell.clubObject = self.clubs?[indexPath.row]
return cell
}
}
PageMenuViewController的代码是
import UIKit
class PageMenuViewController1: UIViewController {
var pageMenu : CAPSPageMenu?
override func viewDidAppear(animated: Bool) {
println("PageMenuViewController1 viewWillAppear")
super.viewDidAppear(animated)
// Array to keep track of controllers in page menu
var controllerArray : [UIViewController] = []
// Create variables for all view controllers you want to put in the
// page menu, initialize them, and add each to the controller array.
// (Can be any UIViewController subclass)
// Make sure the title property of all view controllers is set
// Example:
var controller1 = storyboard!.instantiateViewControllerWithIdentifier("ClubsViewController")! as! ClubsViewController
controller1.title = "CLUBS"
controllerArray.append(controller1)
var controller2 = storyboard!.instantiateViewControllerWithIdentifier("PartiesViewController")! as! PartiesViewController
controller2.title = "PARTIES"
controllerArray.append(controller2)
// Customize page menu to your liking (optional) or use default settings by sending nil for 'options' in the init
// Example:
var parameters: [CAPSPageMenuOption] = [
.MenuItemSeparatorWidth(4.3),
.UseMenuLikeSegmentedControl(true),
.MenuItemSeparatorPercentageHeight(0.1)
]
// Initialize page menu with controller array, frame, and optional parameters
pageMenu = CAPSPageMenu(viewControllers: controllerArray, frame: CGRectMake(0.0, 0.0, self.view.frame.width, self.view.frame.height), pageMenuOptions: parameters)
// Lastly add page menu as subview of base view controller view
// or use pageMenu controller in you view hierachy as desired
self.view.addSubview(pageMenu!.view)
}
}
到目前为止,感谢帮助完成最佳实践。