UITableVIew not working with UINavigationController - objective-c

I have encountered a very strange behavior, when I implemented navigationController.
I have a LogInViewController with an UITableView,where cells are populated like this:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
return cellUsername;
}
if (indexPath.row == 1){
return cellPassword;
}
else{
return nil;
}
}
cellUsername and cellPassword are Cells which I created on .xib..
And when i add this LogInViewController as a rootViewController in my appDelegate it works fine!
I wanted to implement a navigationController, so I created RootViewController, with navController #property and in my appDelagate i called it like this
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
RootViewController *rootView = [[RootViewController alloc]init];
rootView.navController = [[UINavigationController alloc] initWithRootViewController:rootView];
self.window.rootViewController = rootView.navController;
[self.window makeKeyAndVisible];
return YES;
And in my RootViewController on viewDidLoad I have this code:
[super viewDidLoad];
self.navController = [[UINavigationController alloc]init];
LogInViewController *loginView = [[LogInViewController alloc]init];
[[self navigationController] pushViewController:loginView animated:YES];
It opens my LogInViewController,but only the first cell,cellUsername is added to my UiTableView, what is wrong?
Am still a beginner,so I might be missing something stupid probably.. but it works wihout navigationController,so am pretty confused..
Thank you!

I'm not sure if this has anything to do with your problem, but you shouldn't be using this line in your viewDidLoad:
self.navController = [[UINavigationController alloc]init];
Your RootViewController is already embedded in a navigation controller, so you can just use self.navigationController to get a reference to it.
I'm also not sure about this code, it may be ok, but it's not the way it's usually done:
RootViewController *rootView = [[RootViewController alloc]init];
rootView.navController = [[UINavigationController alloc] initWithRootViewController:rootView];
self.window.rootViewController = rootView.navController;
The usual way to do it is like this:
RootViewController *rootView = [[RootViewController alloc]init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:rootView];
self.window.rootViewController = nav;

in AppDelegate file
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
RootViewController *rootView = [[RootViewController alloc]init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootView];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
Further there is no need to create another navigationController in the viewDidLoad of the RootViewController class

Related

Navigation with xib in iOS app

Please, help to understand the navigation. I'm working with xibs. The scheme is: https://www.dropbox.com/s/o82fxpte0hmyxcq/Scheme_Simple.jpg .
Here's my code :
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
FirstViewController *firstViewController = [[firstViewController alloc] initWithNibName:#"firstViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
#implementation FirstViewController
- (IBAction)button1Tapped:(id)sender {
SecondViewController *secondViewController = [[SecondViewController alloc] init];
secondViewController.title = #"View2";
[self.navigationController pushViewController:secondViewController animated:YES];
}
#implementation SecondViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ThirdViewController *thirdViewController = [[ThirdViewController alloc] init];
thirdViewController.title = #"View3";
if (indexPath.row == 0) {
[self.navigationController pushViewController:thirdViewController animated:YES];
[secondViewTableView deselectRowAtIndexPath:indexPath animated:YES];
}
}
So, I have questions:
Where should I create the next view? In my code view has created in "previous" class: view2 created in FirstViewController, view3 created in SecondViewController etc. All new ViewControllers are inside the method that initiates the navigation, is it right way? I think it's wrong, but the navigation is working.
Problems with headers in the navigation bar. It turns out that the title of view2 is only displayed when moving from view1 to view2, but when going back from view3 to view2 – header disappears. I googled, tried to add self.title = #"name" to viewDidLoad, initWithNibName, viewWillAppear – none of this works.
So I've solved the problem with disappearing title of navigation bar. The problem was in my custom back button: self.navigationItem.title = #"";
It was working and title "Back" from my back button disappeared but also title of navigation bar disappeared too. The right way to make back button untitled is:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStyleBordered target:nil action:nil];
[self.navigationItem setBackBarButtonItem:backButton];

Correct way to pushViewController in this case?

I have a custom UITabBarController. On it I have added 3 view controllers adding a navigationController to each one first. It looks like this
· customTabBarController.m viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
FirstViewController *firstController = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UINavigationController *firstNavController = [[UINavigationController alloc] initWithRootViewController:firstController];
SecondViewController *secondController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
UINavigationController *secondNavController = [[UINavigationController alloc] initWithRootViewController:secondController];
self.viewControllers = [NSArray arrayWithObjects:firstNavController, secondNavController, nil];
[self addLeftButtonImage];
[self addRightButtonImage];
self.selectedIndex = 1;
}
Now, when I am on a view controller and I want to pushViewController, how do I have to call it?

UITabBarController with a UIPopOverController with Multiple Views

I am working on a small app, according to the requirement the app should have a tabBarItem with 3 items. For this I have programmatically created the tabBarController in the AppDelegate.m file and added the 3 different viewControllers, instantiated them and everything is working good. I see the tabBarItems and all views are working. In one of the views lets say in SecondViewController I show a popOverController where I used a UITableView and populate it with items. When I click one of the items it should show another view lets say sendFeedback. Until there everything is working fine, but as soon as this sendFeedback is presented as the modal view, it occupies the whole app i.e it hides the tabBarItem.
I present the important pieces of code here for review:
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
viewController1.title = #"First";
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
viewController2.title = #"Second";
UITableViewController *tableView3 = [[tableViewController alloc]initWithNibName:#"tableViewController" bundle:nil];
tableView3.title = #"Third";
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, tableView3 ,nil];
self.tabBarController.delegate = self;
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
[viewController1 release];
[viewController2 release];
[tableView3 release];
return YES;
}
In my popOverViewController.m file I am checking which row is selected in the table according to that I present the view
#pragma mark - TableView Delegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
sendFeedback *sendEmailViewController = [[sendFeedback alloc]initWithNibName:#"sendFeedback" bundle:nil];
downLoad *downloadFilelViewController = [[downLoad alloc]initWithNibName:#"downLoad" bundle:nil];
if (indexPath.row == 0)
[self presentModalViewController:sendEmailViewController animated:YES];
else
[self presentModalViewController:downloadFilelViewController animated:YES];
}
Can anyone guide me how to overcome this with the multiple views. In case if anyone requires more information from my side I would be glad to provide.
NOTE: It is the same with the other view (downLoad) as well
EDIT: Here is how I am initializing my PopOverController in the AppDelegate.m file
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if([viewController isKindOfClass:[SecondViewController class]]){
NSInteger index = [[self tabBarController] selectedIndex];
CGRect buttonFrame = [[[[[self tabBarController] tabBar] subviews] objectAtIndex:index+1] frame];
PopOverViewController *popoverView = [PopOverViewController new];
popoverView.contentSizeForViewInPopover = CGSizeMake(250, 85);
popover = [[UIPopoverController alloc]initWithContentViewController:popoverView];
NSLog(#"X:%f Y:%f",buttonFrame.origin.x,buttonFrame.origin.y);
[popover presentPopoverFromRect:buttonFrame inView:self.tabBarController.tabBar permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
}
Thanks
Modal view controllers are used to "block" your application and fulfill a task before you can proceed. So modal view controllers are not what you want to use.
Instead wrap your controllers which have to be shown in the popover in a navigation controller. In the tableView:didSelectRowAtIndexPath: method you can push the corresponding view controller to the navigation stack.
To slove your problem:
At the place where you create the popovercontroller initialize it with a new UINavigationController. And the navigation controller you have to initialize with a rootviewcontroller namely PopOverViewController.m.
PopOverController *popoverContentController = [[PopOverController alloc] init];
UINavigationController *navcon = [[UINavigationController alloc] initWithRootViewController:popoverContentController];
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContentController];
And in PopOverController.m
if (indexPath.row == 0)
[self.navigationController pushViewController:sendEmailViewController animated:YES];
else
[self.navigationController pushViewController:downloadFilelViewController animated:YES];

set array .viewControllers from SplitViewController

I'm trying to use a splitview inside a TabBar. By now I have a SplitView in my first TabBarItem. My problem comes when I try to access to a different DetailView or right view in the SplitViewController I have.
I'm trying to do it inside the didSelectRowAtIndexPath: of my Root (or Master) viewcontroller from the SplitView.
Here's the code, where I try to acces to my TabBarController from an AppDelegate object, and change the viewControllers array of my SplitView only changing the second view controller. I always get this crash error, saying that 2nd instance send is unrecognized: -[SecondViewController viewControllers]: unrecognized selector sent to instance 0x6852460
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//object AppDelegate
AppDelegate *myDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//Objecte in Index 0 is my SplitVC
NSArray *barControllers = myDelegate.tabBarController.viewControllers;
if (indexPath.row == 0)
{
SplitVC *temporalSplit = [barControllers objectAtIndex:indexPath.row];
NSArray *mArray = temporalSplit.viewControllers;
FirstViewController *detail = [[FirstViewController alloc]initWithNibName:#"FirstViewController" bundle:nil];
UINavigationController *navigationDetail = [[UINavigationController alloc]initWithRootViewController:detail];
temporalSplit.delegate = detail;
temporalSplit.viewControllers = [[NSArray alloc] initWithObjects:[mArray objectAtIndex:0], navigationDetail, nil];
myDelegate.tabBarController.viewControllers = [NSArray arrayWithObjects:temporalSplit.viewControllers, [barControllers objectAtIndex:1], nil];
}
else if (indexPath.row == 1)
{
SplitVC *temporalSplit = [barControllers objectAtIndex:indexPath.row];
NSArray *mArray = temporalSplit.viewControllers;
Detail2VC *detail = [[Detail2VC alloc]initWithNibName:#"Detail2VC" bundle:nil];
UINavigationController *navigationDetail = [[UINavigationController alloc]initWithRootViewController:detail];
temporalSplit.delegate = detail;
temporalSplit.viewControllers = [[NSArray alloc] initWithObjects:[mArray objectAtIndex:0], navigationDetail, nil];
myDelegate.tabBarController.viewControllers = [NSArray arrayWithObjects:[barControllers objectAtIndex:0], temporalSplit, nil];
}
[myDelegate release];
}
And my AppDelegate code (that works without problems):
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
// Override point for customization after application launch.
NSMutableArray *controllersBar = [[NSMutableArray alloc]init];
UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil] autorelease];
for (int i = 0; i<2; i++)
{
if(i == 0)
{
_firstViewController = [[[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil] autorelease];
_masterApp = [[MasterVC alloc]initWithNibName:#"MasterVC" bundle:nil];
_masterApp.firstViewController = _firstViewController;
_firstViewController.mastervc = _masterApp;
UINavigationController *navigationMaster = [[UINavigationController alloc]initWithRootViewController:_masterApp];
UINavigationController *navigationDetail = [[UINavigationController alloc]initWithRootViewController:_firstViewController];
_splitVC = [[SplitVC alloc] initWithNibName:nil bundle:nil];
//_splitVC.tabBarItem = controller.tabBarItem;
_splitVC.viewControllers = [NSArray arrayWithObjects:navigationMaster, navigationDetail, nil];
_splitVC.delegate = _firstViewController;
[controllersBar addObject:_splitVC];
}
else if (i == 1)
{
[controllersBar addObject:viewController2];
}
}
//self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
_tabBarController.viewControllers = controllersBar;
_tabBarController.selectedIndex = 0;
_tabBarController.delegate = self;
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
What am I doing wrong? Any help or suggestion is welcome.
Thanks to all in advance.
Split view controller MUST be root level controllers:
UISplitviewcontroller not as a rootview controller
If not, you can apparently get all sorts of strange things happening.

Issue with UITabBar, multiple UINavigationController and device orientation

I have a UITabBar with 2 UINavigationController created in my AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
UINavigationController *navController1, *navController2;
PolyPage *viewController1 = [[PolyPage alloc] init];
PolyPage *viewController2 = [[PolyPage alloc] init];
navController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
navController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navController1, navController2, nil];
...
return YES
}
My view controllers are the same. In PolyPage.m I have the method :
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:CGPointMake(150, self.tableView.contentOffset.y + 240)];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
imageViewer = [[ImageViewer alloc] initWithDataSource:[NSArray arrayWithObject:cell.imageView.image]];
[self.navigationController presentModalViewController:imageViewer animated:YES];
}
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
This method shows my modal view controller only with the first navigation controller in the UITabBar.
I would like to make it work with both navigation controllers.