Itai Spector
Itai Spector

Reputation: 652

TableViewController inside TabViewController inside NavigationViewController not showing navigation title and item in runtime, but shows in storyboard

I have a TableViewController inside a TabViewController inside a NavigationController with navigation title and item, as seen here:
enter image description here

On runtime, navigation bar is there, but title and add item are not shown, as seen here: (don't mind the white emptiness, in my device its working, but not in the simulator - I asked about it in stackOverflow, but still no solution)
enter image description here
The other TabBar item is pointing at a regular UIView, which is also not showing navigation bar title and bar item.

I tried to add another UIView, which will be a mediator between TabBarViewController and TableViewController, and that fixed the problem, but that made me lose the TabBar functionality in my TableViewController. I tried to point at the navigation bar from my TableViewController, but methods like "viewDidAppear" or navigationBar Class are not accessible from there, as it is acting as a controller for tableView.

TableViewController Class

import UIKit
import Parse
import ParseUI

class MeetsTableViewController: PFQueryTableViewController {

// Initialise the PFQueryTable tableview
override init(style: UITableViewStyle, className: String!) {
    super.init(style: style, className: className)
    //self.tableView.contentInset = UIEdgeInsetsZero;
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    // Configure the PFQueryTableView
    self.parseClassName = "Meets";
    self.pullToRefreshEnabled = true;
    self.textKey="city";
    self.paginationEnabled = false;

}


// Define the query that will provide the data for the table view
override func queryForTable() -> PFQuery {
    let query = PFQuery(className: "Meets");
    query.orderByDescending("numberOfComming");
    return query;
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell {

    var cell = tableView.dequeueReusableCellWithIdentifier("MyCell") as! CarTableViewCell!;

    if cell == nil {
        cell = CarTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "MyCell");
    }
    if let cityName = object?["city"] as? String{
        cell?.meetName?.text = "מפגש ב\(cityName)";
    }
    if let address = object?["address"] as? String{
        cell?.meetAddress?.text = address;
    }
    if let date = object?["date"] as? String{
        cell?.meetDate?.text = date;
    }
    if let time = object?["time"] as? String{
        cell?.meetTime?.text = time;
    }
    if let people = object?["numberOfComming"] as? Int{
        cell?.peopleAttending?.text = "\(people)";
    }

    if let thumbnail = object?["meetImg"] as? PFFile {

        thumbnail.getDataInBackgroundWithBlock{
            (imageData, error) -> Void in
            if error == nil {
                let image = UIImage(data: imageData!)
                cell.meetImage.image = image
            }
        }
    }

    return cell;
}

//delegate method
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    moveToDetail();
}

func moveToDetail(){
    let meetDetail=storyboard?.instantiateViewControllerWithIdentifier("meet") as! MeetDetailViewController;

    if let indexPath = self.tableView.indexPathForSelectedRow {
        let row = Int(indexPath.row);
        meetDetail.currentObject = (objects?[row] as? PFObject)
    }
    navigationController?.showViewController(meetDetail, sender: self);
}

}

TabViewController Class

import UIKit

class MeetingsTabViewControllerViewController: UITabBarController {

override func viewDidLoad() {

    super.viewDidLoad()
    // set red as selected background color
    let numberOfItems = CGFloat(tabBar.items!.count)
    let tabBarItemSize = CGSize(width: tabBar.frame.width / numberOfItems, height: tabBar.frame.height)
    tabBar.selectionIndicatorImage = UIImage.imageWithColor(UIColor(hexString: "#8c332b")!, size: tabBarItemSize).resizableImageWithCapInsets(UIEdgeInsetsZero);
    tabBar.backgroundImage = UIImage.imageWithColor(UIColor(hexString: "#c1463e")!, size: tabBarItemSize).resizableImageWithCapInsets(UIEdgeInsetsZero);
    // item selected color
    tabBar.tintColor = UIColor.whiteColor();
    // remove default border
    tabBar.frame.size.width = self.view.frame.width + 4;
    tabBar.frame.origin.x = -2;

}
}

extension UIImage {

class func imageWithColor(color: UIColor, size: CGSize) -> UIImage {
    let rect: CGRect = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContextWithOptions(size, false, 0);
    color.setFill();
    UIRectFill(rect);
    let image: UIImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

}

NavigationViewController Class

import UIKit

class MeetsNavigationViewController: UINavigationController {

override func viewDidLoad() {
    super.viewDidLoad()

}



}

Upvotes: 1

Views: 280

Answers (1)

Itai Spector
Itai Spector

Reputation: 652

Solved
It appears that adding items to the navigation bar from the tab bar childs is not possible, but adding items to the tab bar controller is possible, so this is what I did. And I just changed to the title according to my selected tab, I added this method to the tab bar controller:

override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
    if item.tag==0{
        navigationItem.title="המפגשים שלי";
    }else{
       navigationItem.title="כל המפגשים";
    }
}

Upvotes: 2

Related Questions