Navigation Bar logic implementation - iphone

I have taken navigation bar from controls and added one right bar button it is also taken from controls my requirement is I just want hide right bar button when view will appear and I have wrote drop down action for right bar button . I have an extra view controller called list view controller in this view controller I have 3 objects when I select one object then it is to be push to main view controller and right bar button is to be visible and when I click on right bar button selected object is to be add in drop down list can any one help me.
[self loadMenu];
[self btn];
self. navigationItem.title = #"sample";
[super viewDidLoad];
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

I'm a bit confused on what you're asking for, but I believe that all you should have to do is
self.navigationController.navigationItem.rightBarButtonItem.enabled = NO;
This will hide the button because it is set to the not-enabled set....
So, for going from one view to another, just do
[navController pushViewController:viewController animatedYES];
and to go backwards from that view do
[navController popViewControllerAnimated:YES];
now to add it altogether:
// creating UIViewController objects for reference later
View1 *view1 = [[View1 alloc] init];
View2 *view2 = [[View2 alloc] init];
// you are setting the view1 right navigation button in the view1 viewDidLoad
self.navigationController.navigationItem.rightBarButtonItem.enabled = NO;
// now you are pushing to second view
[navController pushViewController:view2 animated:YES];
// now, in the view2 class, where you get the image that sends the
// user back to view 1, just do
[view2.navController popViewControllerAnimated:YES];
// and then wherever you do the pop as shown above, you need to set the
// view1 right bar button to enabled
view1.navigationController.navigationItem.rightBarButtonItem.enabled = YES;
Hope this helps!

Related

displaying navigation bar when switching to another view

I have a button when it pressed, I want it to take me to another view (the "news" view). Within the news view, I want there to be a navigation bar with a back button. I have a navigationcontroller setup throughout my app but I can't seem to get this to work when this button is pressed. It takes me to the view I want but there is no navigation bar and no back button. This is my code that is implemented when the button is pressed.
If anybody know what I am doing wrong, it would be much appreciated.
Thanks
-(IBAction)news
{
newsViewController *view1 = [[newsViewController alloc] initWithNibName:#"newsViewController" bundle:nil];
view1.title=#"news";
[self.navigationController pushViewController:view1 animated:YES];
}
I am not in my mac, so I can not test code, but if it is working and the only issue you got is not show the bar, what you need to is set the bar to be visible:
From apple docs:
The navigation toolbar is hidden by default but you can show it for
your navigation interface by calling the setToolbarHidden:animated:
method of your navigation controller object. If not all of your view
controllers support toolbar items, your delegate object can call this
method to toggle the visibility of the toolbar during subsequent push
and pop operations.
Something like that is supposed to work:
-(IBAction)news {
newsViewController *view1 = [[newsViewController alloc] initWithNibName: #"newsViewController" bundle:nil];
view1.title=#"news";
[self.navigationController pushViewController:view1 animated:YES];
//Add this line!
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
I hope it can help you.
write the below code in page where you want to show navigation controller
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = NO;
}

How can I go back to the first view from the third view directly?

When I touch cancel button in the third view, I want to go back to the first view directly. How can I do that?
This is the code.
// this part is in the first view.
SecondController *aSecondController = [[SecondController alloc] init];
UINavigationController *aNaviController = [[UINavigationController alloc] initWithRootViewController:aSecondController];
self.naviController = aNaviController;
[aNaviController release];
[aSecondController release];
[self.view addSubview:naviController.view];
// this part is in the second view.
ThirdController *thirdController = [[ThirdController alloc] initWithStyle:UITableViewStyleGrouped];
[self.navigationController pushViewController:thirdView];
[thirdView release];
// this part is in the third view.
- (void)cancel {
[self.navigationController popViewControllerAnimated:NO]; // this only goes to the second view.
}
popToViewController, popToRootViewController only go to the second view also.
You can use popToRootViewController:animated: method, if your root view controller is the one you're after. You can also use popToViewController:animated: to specify which controller you want to end up with on the top of the navigation stack.
Use UINavigationControllers -popToRootViewControllerAnimated:
- (void)cancel {
[self.navigationController popToRootViewControllerAnimated:NO];
}
And if you ever want to pop to a specific view controller you can use popToViewController:animated: and use the viewControllers property to get the view controller at the correct index.
Set up the navigation controller in your app delegate. Make the first view controller the nav controller's root controller instead of having the first view controller own the nav controller. Then you can use -popToRootViewController:animated: as the other answers have suggested.

Animate New View from Tab Bar Application Template XCode

I have created a new Tab Bar Application template in xcode. How should I properly setup a new view that animates in (Slide Up) when a button is pressed? I've seen this done using the NavigationController.
I've added a navigation bar and a button with an action that adds a sub view using this code:
(IBAction)newPost:(id)sender
{
// Load UIViewController from nib
PostViewController *screen = [[PostViewController alloc] initWithNibName:#"PostViewController" bundle:nil];
// Add to UINavigationController's stack, i.e. the view for this UITabBarController view
[self.view addSubview:screen.view];
// Release music, no longer needed since it is retained by the navController
[screen release];
}
I'm not sure if this is what you're asking, but if you just want a new view controller to present itself modally upward just do this.
1.) New File --> View Controller
2.)
newViewcontroller *viewController = [[newViewcontroller alloc] initWithNibName:#"newViewcontroller" bundle:nil];
[self presentModalViewController:viewController animated:YES];
[viewController release];

Navigation Controller + segment Controller : Unable to change the view

I have a root view controller which has few buttons. On click of these button, I shown up a tab bar controller with each controller showing the table view controller. I have achieved this. So basically, for each table view controller I have a dedicated class. To go ahead, I replaced the tab bar controller, and added the segmented controller to the navigation title view.
The question is how can I set the view based on the selected index. I am able to set the navigation title to be segmented control but on select I am unable to set the view.
Below is what i have achieved so far.
Note: What matters is a running code, I would do that code optimization later on. I dont want to hidde views. I want to call different view controller class.
RootViewController class (on click of the button, i call the first view controller. So that I can set the segment controller:
-(IBAction) pushSegmentController: (id) sender
{
NSLog(#"My Location Button being clicked/touched") ;
FirstViewController *firstViewController = [[FirstViewController alloc] init] ;
[self.navigationController pushViewController:firstViewController animated:YES];
// Releae the view controllers
[firstViewController release];
}
IN FirstViewController class:
-(void)viewDidLoad {
[super viewDidLoad];
NSArray *viewControllers = [self segmentViewControllers];
self.segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Table1", #"Table2"]];
self.navigationItem.titleView = segmentedControl;
[self.segmentedControl addTarget:self
action:#selector(indexDidChangeForSegmentedControl:)
forControlEvents:UIControlEventValueChanged];
self.segmentedControl.selectedSegmentIndex = 0;
}
-(void)indexDidChangeForSegmentedControl:(UISegmentedControl *)aSegmentedControl {
NSUInteger index = aSegmentedControl.selectedSegmentIndex;
if(index ==0) {
UIViewController *table1Controller = [[AustraliaViewController alloc] initWithStyle:UITableViewStylePlain];
**???? HOW SHOULD I SET THE VIEW OVER HERE... AS ITS A PART OF THE NAVIGATION CONTROLLER**
}
else { }
}
Note: I have tried using this option:
[navigationController setViewControllers:theViewControllers animated:NO];
But this option doesnt give me the right result. How should I go ahead with the same as I want to call a view controller class and set its view based on the selected index.
You probably don't want to have one view controller with different views depending on the button index, especially since you already have view controllers for your different screens.
If you want the table view controller to be pushed onto your navigation controller, so it will have a back button that gets you back to FirstViewController, use
-(void)indexDidChangeForSegmentedControl:(UISegmentedControl *)aSegmentedControl {
NSUInteger index = aSegmentedControl.selectedSegmentIndex;
UIViewController *newViewController = nil;
if(index ==0) {
newViewController = [[AustraliaViewController alloc] initWithStyle:UITableViewStylePlain];
} else {
newViewController = [[YourOtherViewController alloc] initWithStyle:UITableViewStylePlain];
}
[self.navigationController pushViewController:newViewController animated:YES];
}
If you'd rather have it slide in from the bottom and you want to handle setting up all necessary user interface (e.g. a dismiss button), replace that last line with
[self presentModalViewController:newViewController animated:YES];
What about?
self.view = table1Controller;
or
[self.view addSubview:table1Controller];
I just saw one more mistake you have done. You are allocating a UIViewController, but initializing it like a tableViewController(with initWithStyle).
If it's a subclass of UITableViewController, alloc it with that, not UIViewController.

Keep Toolbar at top of UIView when using pushViewController and then popViewController?

Update: Another solution to this problem would be if the Navigation bar at the root level of the navigation controller could be transparent or not shown. Is there a way to make the Navigation bar at the root level of the navigation controller transparent or not shown?
I have a NIB with a toolbar at the top of my top level UIView and below the toolbar is a tableView. When I use pushViewController on a navigationController to push another UIViewController onto the navigationController, the toolbar is overwritten by the navigation bar. When I pop the current view back to the root view, the toolbar cannot be seen as there is a blank bar across the top. There is now also a gap between the toolbar and the top of the tableView about the size of the toolbar. So the view looks like from the top:
1) shaded blank bar,
2) blank space about the size of a toolbar,
3) tableview
How can I make the toolbar of the top level NIB appear at the top of the UIView after using popViewController?
In the top level view I instantiate a UINavigationController:
self.navigationController = [[UINavigationController alloc] initWithRootViewController:ListsController];
and then in didSelectRowAtIndexPath I push a view controller
ItemsController * Items = [[ItemsController alloc]
initWithNibName:#"Items" bundle:nil] ;
[self.navigationController pushViewController:Items animated:YES];
To get the initial pushed view to display I do the following:
UIView *navView = self.navigationController.view;
CGRect navFrame = navView.frame;
// navFrame.origin.y -= 20;
navView.frame = navFrame;
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
UIWindow *appWindow = appDelegate.window;
[appWindow addSubview:navView];
Any idea how I can get the top level toolbar not to be overwritten when popping back to the top level?
The solution is to make the navigation bar hidden at the root level using:
- (void)viewWillAppear:(BOOL)animated {
[[self navigationController] setNavigationBarHidden:YES animated:YES];
[super viewWillAppear:animated];
[self.table reloadData];
}
Then at the next lower level of the navigation stack, make the navigation bar unhidden:
- (void)viewWillAppear:(BOOL)animated {
[[self navigationController] setNavigationBarHidden:NO animated:YES];
[super viewWillAppear:animated];
[self.table reloadData];
}