Issues with StoryBoard ViewControllers in IOS Tabbed App
In the struggles of developing an IOS Tabbed App with Swift 1.2 and Xcode 6.3 based on MVC, I use Storyboard visuals instead of doing it programmatically because I'm not an experienced developer. In the image below, you can see the Architecture in the StoryBoard
application:
The application consists of:
- One
TabBarController
to fourTabBar Items
. - Each
Item
has its ownViewController
cStoryBoard
. - They are all associated with
relationship seque
(ViewControllers) inStoryBoard
. - Everyone
ViewController
inStoryBoard
has its ownClass
. - The last item is built in
NavigationController
because I am using the PageMenu project https://github.com/uacaps/PageMenu to enable a paging menu controller with two childrenViewControllers
The problems I have up to this point:
- The two are
child ViewControllers
not related to LastTabBar Item
inStoryBoard
, as you can see in the picture above, only created inparent ViewController Class
(PageMenuViewController1), usually this onePageMenu
works but sometimes the last oneTabBar Item
disappears, I am very confused about this problem . -
override func viewWillAppear
the defaultchild ViewController
is called twice the first time I have println ("ClubsController viewWillAppear").
ViewControllers Code
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 Code
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)
}
}
Appreciate help to achieve best practices up to this point.
source to share
I'm not familiar with CAPSPageMenu, but there is nothing wrong with the scenes in the storyboard not being segue related - it's just a convenience to help with transitions, and instantiating with it is instantiateViewControllerWithIdentifier
completely legal.
Something that stands out on your storyboard is how your table view controller is wired with a navigation view controller.
The navigation controller must be related to the tab bar controller, not the table view controller.
Here is a screenshot of what the connection should look like. This may be why you sometimes lose your tab.
source to share