UITableView in UInavigationController not working - iphone

I have been searching and experimenting on this for a while, but I just can't find out what I'm doing wrong.
I want to make an app with a menu, and from that menu, you can get to a TableViewcontroller with a title bar. I found out that, in order to get this title bar, you need to "insert" UITableViewController into a UINavigationController. this is where I'm stuck.
let's not mind / forget the menu from the app for now, because i know how to switch view controllers when user taps a button.
in my AppDelegate.m I have:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
TableViewController *tableView = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
self.navController = [[UINavigationController alloc] initWithRootViewController:tableView];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
in my AppDelegate.h file I Have:
#interface AppDelegate : UIResponder <UIApplicationDelegate>{
UINavigationController *navcontroller;
}
#property (strong, nonatomic) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navController;
I have 2 classer, UITableViewController and NavigationView.
In the UITableViewController, I made an array called tableRows:
tableRows = [[NSArray arrayWithObjects:#"1", #"2", nil] init];
the numberOfSectionsInTableView is set as 1 and the numberOfRowsInSection is set as return tableRows.count;
I left my NavigationView.m untouched, except that I tried to set a title in it.
in my NavigationView.xib, I tried to make the connection between the UITableViewController and the UINavigationController.
I added an NSObject, and changed its class to AppDelegate
I replaced the View for a UIWindow and connected the window property from my
AppDelegate.h to it
I added a UINavigationController and
connected it with the navController from my AppDelegate.h.
and last, I changed the class from the RootView from the
UINavigationController to UITableViewController.
Now my problem is, with or without the connections in IB, whatever I try to change in my table, or in the titlebar, it does not change when I run the app.
Does anybody know what I'm doing wrong? I'm using xcode 4.6, so lots of the tutorials I've checked are not very useful, because they are made with older versions of xcode. please help me, Thank you in advance!

in you AppDelegate replace this line
TableViewController *tableView = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
with this
TableViewController *tableView = [[TableViewController alloc] initWithStyle:UITableViewStylePlain];

You're not creating an instance of TableViewController but instead UITableViewController so thus you're not getting any of your code executed. (You should have a compiler warning for this in your app delegate - make sure to heed those warnings). Likewise you're not loading the NavigationView nib anywhere - you're creating a navigation controller with the table as the root view controller. It's not clear what your objective for NavigationView is so I can't provide a recommendation for how to proceed. However, for the table view, change the following line in your app delegate
TableViewController *tableView = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
to
TableViewController *tableView = [[TableViewController alloc] init];
In your table view xib, make sure File's Owner's class is set to be of class TableViewController (not UITableViewController). Right click on the table view and drag the circles next to 'data source' and 'delegate' to file's owner. Then right click file's owner and make sure the circle next to 'view' is connected to the table view. Save, clean, and run.

You might also find it very useful to checkout the free Sensible TableView framework. The framework will take your array and automatically generate all the table view cells, while managing all the details required to correctly setup the navigation controller. Hope this helps.

Related

Tab bar display through out the project?

I have a Tab-bar application in iOS Storybords. The UITabbarcontroller is connected to -->4 sets of (UInavigation controller--> UItableview controller). Each UITableviewcontroller cells are connected to multiple UIviewcontrollers to be pushed.
On building the app -The navigation tab appears on the top of the view controllers without any problem. But the Tabbar with 4 items at the bottom is visible ONLY on the first view. The UIviewcontrollers does not display the 4 item Tabbar!??. I have set the bottom bar to tab bar in the attributes inspector. But it doesn't work?
I believe there must be more to it than I understand. Hope somebody helps.
How to display the tabbar through out the app?
I'm not totally sure of your design, but here are a couple of tips that hopefully may help.
storyboard design:
basic tabbar application layout would generally look like this from left to right with the first controller on the left having the little arrow indicating it is the starting controller.
your first controller should be the tabbar controller
each tab should be connected to a navigation controller
each navigation controller should be connected to one or more UIViewControllers or UITableViewControllers.
now, note while there are advanced configurations, this is just the general layout that sets up the app nicely and gives easy push transitions for each tab.
if your app starts off and shows the tabbar and when you select a tab item it should show the view controller for that tab. if the tabbar is still there, then you are in good shape up to that point. If you select something on that view controller and it pushes a new view controller on screen and when that happens you lose the tabbar bar, then its likely this is the issue:
- check your view controllers and look in the object inspector for a check mark called "hides bottom bar on push" - if it is checked - then uncheck it.
If you don't find it there, then inspect your code for your view controllers and look in the startup methods like view did load for the statement: self.hidesBottomBarWhenPushed = YES; if you find that command, comment it out or delete it.
It is perfectly ok to hide the tabbar on some view controller pushes if that is your design and makes sense in your application. Generally, it's a good practice to try and avoid it and leave the tabbar on screen when possible for user experience but sometimes issues like screen size might lead the developer down a path of hiding it for some workflow.
I hope this helps and ties back to your question. if not, sorry.
You can add tab bar like this way:-
Appdelegate.h
#import <UIKit/UIKit.h>
#class StartingViewController;
#interface Appdelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
StartingViewController *viewController;
UITabBarController *tabBarController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet StartingViewController *viewController;
#property (nonatomic,retain) UITabBarController *tabBarController;
-(void)addTabBarToView;
#end
app delegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the view controller's view to the window and display.
[window addSubview:viewController.view];
[window makeKeyAndVisible];
[self addTabBarToView];
return YES;
}
-(void)addTabBarToView{
FirstController *first = [[FirstController alloc] initWithNibName:#"FirstController" bundle:nil];
first.title = #"First";
SecondController *second = [[SecondController alloc] initWithNibName:#"SecondController" bundle:nil];
second.title = #"Second";
ThirdController *three = [[ThirdController alloc] initWithNibName:#"ThirdController" bundle:nil];
first.title = #"Third";
Forthcontrooler *Four4 = [[Forthcontrooler alloc] initWithNibName:#"Forthcontrooler" bundle:nil];
second.title = #"Secfor";
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:first];
UINavigationController *navigationController2 = [[UINavigationController alloc]initWithRootViewController:second];
UINavigationController *navigationController3 = [[UINavigationController alloc] initWithRootViewController:three];
UINavigationController *navigationController4 = [[UINavigationController alloc]initWithRootViewController:Four4];
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController1,navigationController2,navigationController3,navigationController4,nil];
[window addSubview:tabBarController.view];
}
Put the UINavigationControllers inside the UITabbarControllers instead of the other way around

page-based app with uitableview; how to show detail view and show another data in the table when goes to next page?

I have page-based app. On each page I have 3 uibuttons at the top, uiscrollview with alphabet (uibuttons to sort data in uitable) at the right and uitableview at the center. How to show cell's detail view? If it is necessary to add uinavigationcontroller I can't do this. If I adds it, it disables interaction with my table, buttons and scrollview.
And another question is how to show new data in tableview and scrollview when goes to next page??
I have rootViewController class and DataViewController class.
rootViewController listing:
#interface RootViewController ()
#property (readonly, strong, nonatomic) ModelController *modelController;
#end
#implementation RootViewController
#synthesize pageViewController = _pageViewController;
#synthesize modelController = _modelController;
#synthesize navContr = _navContr;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Configure the page view controller and add it as a child view controller.
//[self presentModalViewController:navContr animated:YES];
self.pageViewController = [[[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil] autorelease];
self.pageViewController.delegate = self;
DataViewController *startingViewController = [self.modelController viewControllerAtIndex:0 storyboard:self.storyboard];
NSArray *viewControllers = [NSArray arrayWithObject:startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:NULL];
self.pageViewController.dataSource = self.modelController;
[self addChildViewController:self.pageViewController];
[self.view addSubview:self.pageViewController.view];
self.navContr = [[UINavigationController alloc] initWithRootViewController:self.pageViewController];
[self.view addSubview:self.navContr.view];
// Set the page view controller's bounds using an inset rect so that self's view is visible around the edges of the pages.
CGRect pageViewRect = self.view.bounds;
self.pageViewController.view.frame = pageViewRect;
[self.pageViewController didMoveToParentViewController:self];
// Add the page view controller's gesture recognizers to the book view controller's view so that the gestures are started more easily.
self.view.gestureRecognizers = self.pageViewController.gestureRecognizers;
for (UIGestureRecognizer *recognizer in self.pageViewController.gestureRecognizers){
if ([recognizer isKindOfClass:[UITapGestureRecognizer class]]){
[recognizer setEnabled:NO];
}
}
}
After few manipulations it works but I need help to get it work fine!
So now it looks like this
Next question: how do I remove the brown space at the top???
::UPDATE::
Problem solved. It needs just to set y-axis position of UINavigationController to -20 ;)
i'm not sure if this link about creating navigation-based project may help you.. (http://iosmadesimple.blogspot.com/2012/08/navigation-based-project-doing-it.html)
From that tutorial, there's a class called SampleViewController, subclass of UIViewController. You might want to put a tableView in SampleViewController.xib file. Then in your SampleViewController.h file, add an IBOutlet UITableView* yourTable property and synthesize it. Connect it to your tableView in your .xib file. //Or you may do it programmatically
in your SampleViewController.h, make your interface header look like this.. I think you already know this...
#interface SampleViewController:UIViewController < UITableviewDelegate, UITableViewDatasource >
in your SampleViewcontroller.m, under viewDidLoad method, set the table delegate and datasource to self:
yourTableView.delegate = self;
yourTableView.datasource = self;
Afterwhich, you implement tableView delegate and datasource methods... //you already know those since you were already able to show a tableview ;)
one of these methods is the "tableview:didSelectAtIndexpath:" --> this is the part wherein you can put your code when you click one of the cells.
Let's assume you have the DetailsViewController Class, this is the class you would like to show after clicking a cell and show its details.
DetailsViewController Class must have a variable that will accept the data you would like to show. Let's say, an NSString *detailsMessage; //do the #property and #synthesize thing...
Let's go back to SampleViewController.m file, under tableview:didSelectAtIndexpath: Method:
inside that Method.. put these codes.
DetailsViewController *detailsVC = [[DetailsViewController alloc] init];
detailsVC.detailsMessage = #"The Data you want to pass.";
[self.navigationController pushViewController:detailsVC animated:YES];
I hope this helps. :(
There may be other ways but by far the easiest way is to use a navigation controller. In fact, it is built to do exactly this.
If you don't want the navigationBar then you can hide it in the viewWillAppear function.
[self.navigationController setNavigationBarHidden:YES animated:YES];
Then you can add an other UIViewController to push when the user selects a cell.
Having read your OP again I'm not sure how you are adding your navigationController.
To use a navigationController you create it and load it at start time. You then create your current viewController (the one with the buttons and table etc...) and set this as the rootViewController of the navigationController.
Then you display the navigationController.
Could you explain how you are adding your navigationController as it may help understand what is going wrong.
Thanks
::EDIT::
OK, my assumption was correct.
The way you are using the navigation controller is not how it was intended.
OK, so at the moment your AppDelegate file will have a method Application didFinishLaunching...
It will look something like this...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[OJFViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
You should change it so that it is like this...
first add a property to your appDelegate...
#property (nonatomic, strong) UINavigationController *navigationController;
Then change the didFinishLaunchingMethod to this...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[OJFViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
This will still show you MainViewController but it will now be contained within a navigationController.
Next in your MainViewController function viewWillAppearAnimated add the line...
[self.navigationController setNavigationBarHidden:YES animated:animated];
This will then hide the navigationBar at the top of the view so you still have access to your buttons.
The you need a new ViewController and xib file (for example DetailViewController).
When the user selects a table row you need to then do something like...
DetailViewController *detailView = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
//pass in details of which row was selected.
[self.navigationController pushViewController:detailView animated:YES];
This will then display your new view and new viewController. You will also need to write a way of passing the data in (set up a property on the DetailViewController).
Hope this helps.

Advice with Tab Bar and Nav Bar

I'd just like to clear something up..
I have an app where the Main Window UI has a Tab bar with 3 tabs (opt1, opt2, op3). Each opt has its own xib file where i've drawn their own interfaces.
In my app delegate class I have included a UITabBar *rootController, and hooked this up to my tab bar in my Main Window xib file.
Now.. In the Tab bar, I have dragged in 3 navigation controllers (1 for each opt) and inside each one I have a 1) tab bar icon, 2) navigation bar and 3) view controller.
Back in my app delegate.h class I have included code for UINavigationController *nav1, nav2, nav3..and hooked these up accordingly in IB in MainWindow.xib (TabBar->navController1, navController2, navController3).
Is this the right way to do it? Also how can I make use of these nab bars in my opt1, opt2, opt3 class files?
here is my code:
app delegate.h
#import <UIKit/UIKit.h>
#class LoginViewController;
#interface myAppDelegate : NSObject <UIApplicationDelegate>
{
UIWindow *window;
UINavigationController *navigationController1, *navigationController2, *navigationController3;
IBOutlet UITabBarController *rootController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController1, *navigationController2, *navigationController3;
#property (nonatomic, retain) IBOutlet UITabBarController *rootController;
#end
appdelegate.m
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview:[rootController view]];
[window makeKeyAndVisible];
LoginViewController *loginViewController = [[LoginViewController alloc] initWithNibName:#"LoginView" bundle:nil];
[self.rootController presentModalViewController:loginViewController animated:NO];
}
Then in my LoginController.m class , when the user enters correct credentials I call
[self dismissModalViewControllerAnimated:YES];
In my MainWindow.xib, I hook up my rootController to a TabBarController. In the TabBarController I have put 3 NavigationControllers inside it and linked them to 3 tabOption classes which each have their own .xib view.
The tab bar switches between the 3 option views nicely. However in 1 .xib view I have a button to open a new .xib. So in my tabOption1 class I have the following:
-(IBAction)openBook:(id)sender{
UIViewController *nextVC = [[PageViewController alloc] initWithNibName:#"PageView" bundle:nil];
[self.navigationController pushViewController:nextVC animated:YES];
}
However this does not open up my PageView.xib... I have connected it to my PageViewController class and everything too..and the button works because I've tested it with a UIDialog
Have you seen the Apple Programming Guides? They might give you a better understanding of how everything ties together - you could start here:
http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/NavigationControllers/NavigationControllers.html#//apple_ref/doc/uid/TP40007457-CH103-SW1
In answer to your question, that looks like an OK way of setting up. I really would recommend reading up a bit though :)
In response to your comment, that looks like a reasonable way to do what you're trying to achieve. If it works, then it works.
In response to your other issue then you can get the navigation controller object by doing this: self.navigationController
So you can "go to" a new view controller like this:
// make the view controller
UIViewController *nextVC = [[MyCustomViewController alloc] initWithNibName:#"MyCustomViewController" bundle:nil];
// push it onto the navigation stack
[self.navigationController pushViewController:nextVC animated:YES];
To add this to the click event on a button you need to create the button in interface builder and create an IBAction in your code. The IBAction might look like this:
- (IBAction)pushNextViewController:(id)sender {
UIViewController *nextVC = [[MyCustomViewController alloc] initWithNibName:#"MyCustomViewController" bundle:nil];
[self.navigationController pushViewController:nextVC animated:YES];
}
Then you need to link to it from interface builder. I'm not sure how to do this, I generally don't use interface builder, and certainly haven't used it since about XCode 3.
To do it programatically you can use this method:
[MyButton addTarget:self selector:#selector(pushNextViewController:) forControlEvents:UIControlEventTouchUpInside]; // always use touch up inside
Keywords to look up to help you find tutorials and stuff on the internet: ibaction uinavigationcontroller pushviewcontroller:animated: popviewcontrolleranimated:

UITabBarController is null?

I was trying to create a tab bar based application from scratch but I'm having some issues. Basically I have my AppDelegate.h, AppDelegate.m and a MainView.xib. In the the .h file I have:
#interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) IBOutlet UITabBarController *tabBarController;
#end
In My .m I have:
#synthesize window = _window;
#synthesize tabBarController = _tabBarController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
And in my xib file I have my files owner as a class of Appdelegate and I have my outlet from there hooked up with a Tab Bar Controller. The problem is that my controller is returning null and thus isn't being set as the rootViewController. What step/idea am I missing in setting up my app? Thanks in advance!
EDIT FIXED: Since I was starting from a empty application I didn't have 'Main nib file base name' set, so I just had to set it to the nib I was trying to load.
You need to create the view controllers that will be accessed through the tabs on your Tab Bar Controller. Then you must assign them to the viewControllers property of the Tab Bar. You can do this just before setting the Tab Bar as the window's rootViewController:
MyViewController1 *vc1 = [[MyViewController1 alloc] init];
MyViewController2 *vc2 = [[MyViewController2 alloc] init];
NSArray *controllers = [NSArray arrayWithObjects:vc1, vc2, nil];
self.tabBarController.viewControllers = controllers;
I hope it helps!
Normally the appDelegate object is not a subclass of UIResponder, but a subclass of NSObject.
Are you hooking the TabBar instance to the first responder in your xib file? take into account that this won't work as the first responder is only a proxy object and setting the class type to it won't cause an object to be instantiated.
Instead, create an AppDelegate object in your xib file and hook the TabBar to it.
Hope it helps.
Since I was starting from a empty application I didn't have 'Main nib file base name' set, so I just had to set it to the nib I was trying to load.

how to structure my objects when using uinavconttroller and uitabbar

I am trying to understand what is a proper structure of the objects when using uinavigationcontroller with a tab bar.
I want my app to have the following structure: welcome/login screen -> 3 bar tabs.
I have the following objects/classes:
AppDelegate
WelcomeViewController
TabController
FirstTab
SecondTab
ThirdTab
I have also created a uinavcontroller under WelcomeViewController once the user clicks on "enter" to the app:
-(IBAction)aMethod:(id)sender {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
FirstView *controller = [[FirstView alloc] initWithNibName:#"FirstView" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:controller];
self.window.rootViewController = self.navigationController;
navigationController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:navigationController animated:YES];
}
My question is - how should I manage the tab bar - where should I declare each one of its pieces, and will I need to create a uitabbarcontroller in this case (in which case, where?)).
I am very confused as to how to place the different tab bar related declarations and none of the examples/ tutorials our there were able to clarify this for me.
BTW - I started this app from a view based application.
Thanks!
You can either set this up in code or you can do it using interface builder.
I prefer the interface builder method as you can visually see the structure of your view controllers.
This is how I do it...
In your AppDelegate.h add a property
#property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
In your AppDelegate.m firstly synthesize the property
#synthesize tabBarController = _tabBarController;
Set up the application:didFinishLaunchingWithOptions: method to look something like this (you may do more work in this method)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self.window addSubview:self.tabBarController.view];
[self.window makeKeyAndVisible];
return YES;
}
In MainWindow.xib drag a Tab Bar Controller object onto your objects area (this is where your AppDelegate and Window objects are).
Ctrl + Drag from the AppDelegate to the Tab Bar Controller object and select the property that we just made.
NOTE: Now we have a Tab Bar Controller set up and ready to roll.
There should be two tabs set up as an example. If you just want to use sub classes of UIViewController then just change the classes of these objects to represent your UIViewController sub classes.
If you want to use UINavigationController then drag a UINavigationController object onto your Tab Bar Controller object.
Now click the disclosure triangle on UINavigationController and change the class of its ViewController to be your custom subclass of UIViewController.