Navigation controller not displaying back button when same class is reused - iphone

I'm doing a navigation based application.
Let me explain a little bit about the application.
At the program load table view will show 3 options like:
Hotel
Restaurants
Cafe
If a user selects any of the above options, it will navigate to another table view and show sub options. And it'll continue up-to 6 to 7 levels.
So I in the didSelectRowAtIndexPath: of the tableView's class (Sales) I created it's own object and using [self.navigationController pushViewController:myViewController animated:YES]; I'm creating another view and binds data from the database. It's working fine for me, ut the issue is. I'm not getting the 'back' button that is automaticaly displays on the navigation bar.
my implementation is like this
#implementation Sales
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Sales *myViewController = [[Sales alloc] initWithNibName:#"Sales"];
[self.navigationController pushViewController:myViewController animated:YES];
//Do other stuffs here
}
#end
Like this I'm displaying the all levels. But when I navigated inside of any option I didn't get a back button. (Usually if I navigates to Hotel it will show a detailView and shows a back button on the navigation bar, but in this case this is not happening).
I think the issue is with creating objects of the same class and displaying it, but I can't create such a number of classes and views (about 43 levels total).
How can I resolve this issue (How can I show back button on the navigation bar) ?
Is my method is good one or Is there any alternative solutions for this issue?
Thanks in advance.

Your method to push a view controller of the same class is fine. The error must be elsewhere.
Check if the view controller has a title. That is the text that is shown on the back button when the child view controller is visible. If that text is nil there will be no back button.

When you create a "Back" button, it's for the "next" level. Therefore the back button of the "current navigation controller" was created at the previous level... Yes, it's a bit confusing.
Well, I blogged about this at "iLessons iLearned: How to Define Custom backBarButtonItem", but here's sample code for back button creation:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *backButton =
[[UIBarButtonItem alloc]
initWithTitle:#"My Back"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
}
Now just make sure you use it at correct navigationController!

Related

iPhone app, navigationcontroller not functioning?

I have 4 tabs in a tab bar. In one of the tabs i want to use a navigation, i.e. when i click an item from the list it should go to some details page about it. I have the list page where i have the navigation bar and the list of items. I can scroll them, but when I click any of them the selection animation happens, console logs the true row value, it even prints log instructions from constructor of the Details page but I can not see the Details page showing up. (btw Xcode 3.2.6 with iOS 4.3)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *rowValue = [myStrings objectAtIndex:indexPath.row];
NSLog(rowValue);
//[Utility setStr:rowValue];
[self.myTable deselectRowAtIndexPath:indexPath animated:TRUE];
//DETAILS PAGE HERE!!!!
RestViewController * rest= [[RestViewController alloc] init];
rest.scoreLabel.text = rowValue;
[self.navigationController pushViewController:rest animated:TRUE];
[rest release];
}
Anybody having any idea? Thanks in advance!!!
You can only use pushViewController: if your view controller stack is being managed by a UINavigationController. If the viewController you are trying to push onto is not being managed by a Navigation Controller, nothing will happen.
It sounds like this is the case if your app. Be sure your View Controller hierarchy is set up this way:
You have a UITabBarController at the top level.
The tab you are working with should manage a UINavigationController
Your tableViewController should be set as the rootViewController of the navigation controller in #2

Navigate from one view to another using UIButton

I have an application with 2 views . In the first one I have a button which when I clicked the user should go to the second view. I tried what is explained before here from Karoley , but it does not work . When I click the button nothing happened?
Here is the code of my action :
-(IBAction)gotoSecondPage:(id) sender{
NSLog(#"In gotoSecondPage");
LeoActionViewController *aSecondPageController =
[[LeoActionViewController alloc]
initWithNibName:#"LeoActionViewController"
bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:aSecondPageController animated:YES];
}
LeoActionViewCOntroller is a controler for a second view.
It just do not switch to a second view. I do not know why
I put code your problem this will help you. First of all, you declare method and open .xib file and then connect to that button with selected touchupinside connection.
In the .h file:
- (IBAction)gotoSecondPage:(id) sender;
In the .m file:
- (IBAction)gotoSecondPage:(id) sender
{
NSLog(#"In gotoSecondPage");
LeoActionViewController *aSecondPageController =
[[LeoActionViewController alloc]
initWithNibName:#"LeoActionViewController"
bundle:nil];
[self.navigationController pushViewController:aSecondPageController animated:YES];
[aSecondPageController release];
}
I'm not sure in what capacity you want to switch views.
What immediately comes to mind is that you want a Navigation Controller. This is an object that lets you put view controllers on a stack and push and pop them to show and hide them. It creates a navigation pathway through your app and is easy to use. It also facilitates the 'standard' navigation bar which is found in many iphone apps.
If you just want to change one view for another view you can do many things including hiding and showing different views using setHidden:(bool)hidden. Otherwise you can use addSubview:(UIView *)view and removeFromSuperview to add and remove views completely from the superview.

not getting tab view when we press back

hii every one
i have created a test project with tab view controller, on click of a tab it will goto that coresponding screen(say screen A) when i click back button in screen A it will come back to main page but with out tabView,
following is may code for back button where DataEntry is a class name to where i am navigating
DataEntry *avController;
UINavigationController *addNavigationController;
if(avController == nil)
avController = [[DataEntry alloc] initWithNibName:nil bundle:nil];
if(addNavigationController == nil)
addNavigationController = [[UINavigationController alloc] initWithRootViewController:avController];
//avController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.navigationController presentModalViewController:addNavigationController animated:YES];
insted of the above code if i use following code
[self.navigationController dismissModalViewControllerAnimated:YES];
it will work fine & ill get back to main page with tabs , but it wont run the updated code which is in the viewDidLoad so i need to navigate to the main page insted of using dismissModalViewController
can any one tell me how can i get tabView when i navigate to the main page with out using dismissModalViewController
this is not correct:
[self.navigationController presentModalViewController:addNavigationController animated:YES];
(this is not what you mean to have: it will present a modal view, when you dismiss it, it will uncover what was below, i.e. your tab)
you should use pushViewController to show your avController so that the back button is activated and so on.
look at this.
I'm struggling a little to understand your problem, but viewDidLoad only runs when the view loads: not when you come back to screen A : its still loaded then. To run code each time screen A is displayed, take a look at viewWillAppear / viewDidAppear http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html
I tend to create tabs programatically rather than using the IB: its clearer whats going on. Take a look at this answer to show how I do it.
UITabBar with UINavigationController in code

What's the best way to implement a Back button on the Navigation Bar of an iPhone app

Fairly new to the coding community and took over an app from my developer. I want to implement a Back button on the navigation bar of one of the views in my app. If you guys could point me in the right direction, that'd be greatly appreciated.
Thanks very much!
in some scenarios, where you are not using an UINavigationController, you can create a UINavigationBar yourself.
self.navBar = [UINavigationBar new];
self.navBar.contentMode = UIViewContentModeTopLeft;
self.navBar.delegate = self;
self.navBar.frame = CGRectMake(0, 30, mainWindow.frame.size.width,40);
UINavigationItem *backButton = [[UINavigationItem alloc] initWithTitle:#"Back"];
[self.navBar setItems:[NSArray arrayWithObjects:backButton,[[UINavigationItem alloc] initWithTitle:#"Current View"], nil]];
[self.view addSubview: self.navBar];
Your controller should then implement the UINavigationBarDelegate protocol, so that you can respond to
-(BOOL) navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item
where you can go back to your previous view.
There is mostly no use-case for this. You'll probably really want to work with UINavigationControllers and the push- and popViewController methods.
I hope this helped anyway.
kind regards.
The back button is added automatically when you use [UINavigationController -pushViewController]. Basically, instead of having a regular view controller, you need a navigation controller that is initialized with the default view, and when you want to show the other view, you need to push it on the navigation controller. By pushing the new view controller, it automatically adds the back button. The title of the back button is typically the title of the view controller, but you can change it with navigationBar.backItem.title.

Adding a subview into view hierarchy

I'd like to have a view appear when the user clicks a button. The hierarchy I have looks like this:
MainWindow
-UIView
--ScrollView
---ScrollView.pages = UIViews
----UIView (from above assignment)
----TextView
----InfoButton
pages is an NSMutableArry of pageController objects. These hook to a nib. These nibs are the pages that user flicks through in the scroll view.
The InfoButton click is wired up like this:
- (IBAction) infoButton_click:(id)sender{
topView topViewViewController *topView = [[topViewViewController alloc] initWithNibName:#"TopView" bundle:[NSBundle mainBundle]];
//[self.navigationController pushViewController: topViewView animated:YES];
//[self.view addSubview: topViewView.view];
[super.view addSubview: topViewView.view];
[topViewView release];
}
InfoButton is on one of the pages in the ScrollView. I've commented out different code that has been tried. None of it adds the view. Nothing happens. Is there a way to get TopView as the top view in the hierarchy?
Is your goal to add the view as a subview, or to slide on a new view using the navigation controller? I'm going to assume the latter for the moment.
- (IBAction)infoButton_click:(id)sender
{
TopViewController *topViewController = [[TopViewController alloc] initWithNibName:#"TopView" bundle:nil];
[self.navigationController pushViewController:topViewController animated:YES];
[topViewController release];
}
This is correct if you actually have a navigationController. Make sure you actually do. When "nothing happens" in Cocoa, it usually means something is nil. You should check in the debugger or with NSLog() to see if any of these values are nil. It is possible (even likely), that your parent has a navigationController, but you do not.
Classes should always have a leading capital. Do not create a variable called "view" that is of class "UIViewController". This is a sure path to suffering. Objective-C is a dynamic language with limited compiler checks on types. Naming things correctly is critical to effective programming in ObjC.
Based on your comment to a previous answer, you want to present a modal view. You do this by creating a new view "modalView" and calling [topView presentModalViewController:modalView animated:YES].
In a future version of the iPhone OS, which of course I would be unable to comment upon if it were under NDA, you might be able to present a modal view controller with a flip transition by setting a property on the view controller to be presented, which would probably be called modalTransitionStyle or somesuch.