I need to implement the back button; (as in webpages, when you click the back button the screen will set focus to the previous screen).
I am not using a navigation control here, instead i want to do this using a Navigation bar and then adding a navigation button on it. Now when the user clicks on the navigation button the previous view should be displayed.
How should i do this.
My screen looks like this :
When i click on Hello, it should go to the previous screen.
Check out UINavigationController Class Regerence then try something like this:
- (void)pickerCancel {
[self dismissModalViewControllerAnimated:YES];
}
- (void)doSomething {
[myView.navigationItem setBackBarButtonItem:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(pickerCancel)] autorelease]];
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:myView];
[navigation setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[navigation setDelegate:self];
[self presentModalViewController:navigation animated:YES];
[myView release];
[navigation release];
}
You really should use a UINavigationController as that is its entire purpose.
Related
In my application, i tried to use the function
[self.navigationCotroller presentModalViewController:nextVC animated:YES];
However, when it goes to the next view, the subview fulfill the whole screen(of course it did)
And the question is , how could i add the bar button which lead the view back?
i have tried to use
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:#"back" style:UIBarButtonItemStyleDone target:self action:#selector(Go)];
in function viewdidLoad, but it did not work, there's no bar or even button showed
UINavigationBar *bb = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)];
self.NVBar = bb;
[self.view addSubview:bb];
However, i have no idea how to add a barbutton to the new navigationBar ---NVBar
Would you like to give me a solution?
You will need to pop the new view controller in order for the navigation bar to be automatically included.
In the view controller your code from above is in,
//*** Hook up four buttons to these actions and play around with it for a
//*** better sense of what is going on.
//*** JSBViewController is the name of my test class
//*** You should use your ViewController class that you are currently coding so
//*** you can see the difference of pop/push versus present/dismiss inside of a
//*** navigation controller
-(IBAction)push:(id)sender
{
JSBViewController * viewController = [self.storyboard instantiateViewControllerWithIdentifier:NSStringFromClass([JSBViewController class])];
[self.navigationController pushViewController:viewController animated:YES];
}
-(IBAction)pop:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
-(IBAction)present:(id)sender
{
JSBViewController * viewController = [self.storyboard instantiateViewControllerWithIdentifier:NSStringFromClass([JSBViewController class])];
[self presentViewController:viewController animated:YES completion:nil];
}
-(IBAction)dismiss:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
For the behavior you are looking for, you will need to use the push method
[self.navigationController pushViewController:viewController animated:YES];
If you use [self.navigationController pushViewController:picker animated:YES]; to push your nextVC, then the Nav Bar should stay at the top. Then you can pop back with ` [self.navigationController popViewControllerAnimated:YES];
It's quite unconventional to use a nav bar to go back from a modal view.
`
I want to show new contact page on button click I have written this code within action button
{
ABNewPersonViewController *abnewpersonviewcontroller = [[ABNewPersonViewController alloc] init];
[self.navigationController pushViewController:abnewpersonviewcontroller animated:YES];
[newPersonController release];
}
this code succesfully reach me to the new contact page but the problem is navigation bar not shown onto the new contact page in which i can save record and also have cancel button on it kindly tell me how I can display the navigation on new contact page when I click on action button
use this
ABNewPersonViewController *abnewpersonviewcontroller = [[ABNewPersonViewController alloc] init];
self.navigationController.navigationBarHidden = NO;
[self.navigationController pushViewController:abnewpersonviewcontroller animated:YES];
[newPersonController release];
ABNewPersonViewController *picker =[ABNewPersonViewController new];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:picker];
picker.newPersonViewDelegate =self;
[self presentViewController:nc animated:YES completion:nil];
did you hide navigation bar in your application?
if yes, then you should unhide before moving to contact page........
thanks
Well you can add self.navigationController.navigationBarHidden = NO; to that pushed view controller in - (void)viewWillAppear:(BOOL)animated method to force it but it should show automatically if you didn't added extra UINavigationBar to your UINavigationController.
I have password authentication for my application. My application is window based. I have five view: MyApplicationAppDelegate,MainViewController, HelpViewController, SettingViewController and ErrorViewController.
When the application starts, I navigate to MainViewController. I have two buttons on toolbar for Help and Setting which direct to respective pages. Now, I have written following code in MyApplicationAppDelegate:
- (void)applicationWillEnterForegroundUIApplication *)application {
[self showMyView];
}
-(void)showMyView{
if (![Global timeSet]) {
if (errorviewFlag) {
if (![Global show]){
ErrorPageViewController *bViewController = [[[ErrorPageViewController alloc]
initWithNibName"ErrorPageViewController" bundle:[NSBundle mainBundle]] autorelease];
[self setError:bViewController];
self.error = bViewController;
//[window addSubview:[error view]];
[[self navigationController] pushViewController:bViewController animated: NO];
UIBarButtonItem *_backButton = [[UIBarButtonItem alloc] initWithTitle"Back" style:UIBarButtonItemStyleDone target:nil action:nil];
[self.navigationController setNavigationBarHidden:YES];
[self.navigationController setToolbarHidden:YES];
[_backButton release], _backButton = nil;
[window makeKeyAndVisible];
errorviewFlag=FALSE;
[Global setShow:TRUE];
}
}
}
}
Now, this method is called for all times except the first.
The problem is this only works in desired way, i.e. shows ErrorPage, when I minimize the application from MainView. If I minimize from Help or setting, it does not show the error page, but instead the mainpage with no navigation controls. Why?
He was able to find the answer. I used NSNotificationcenter with didEnterBackground. I used this in all other view than MainView. And in my didEnterBackground method i used
[self.navigationController popViewControllerAnimated:NO];
This forced all other view to come to MainView when application is minised and this solved the problem.
For some reason, if I try to go back to the main menu using the back button on the upper left corner, only the title returns to the previous menu, but not the view controller. View controller would return to the previous menu only if I explicitly call popViewControllerAnimated using some other button.
Is there anyway to solve this? I think I've coded something wrong. Tried googling but couldn't find any cases like mine.
I'm getting the exact same problem. Here is my code:
- (IBAction) showGameView:(id) sender {
gameView = [[TCGameViewController alloc] initWithNibName:#"TCGameViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:gameView animated:YES];
[gameView release];
}
And when I am done with gameView, I do this:
[self.navigationController setNavigationBarHidden:NO animated:YES];
But all it does when I push the 'back' button is cycle through the navigation bar, but never pops the view. I don't even know how to debug it.
In my other view, "infoView" I call the same code as before except the NavBar is never hidden, but it works just fine.
helps!
This problem can occur when you override the following method in your custom view controller:
- (UINavigationItem*)navigationItem
But you don't specify a UIBarButtonItem for the leftBarButtonItem property of the returned UINavigationItem.
If you use a custom navigationItem, and want the standard back button functionality, you could add a method as follows (remember that every UIViewController has a reference to the navigationController that containts it):
- (void)backButtonTapped
{
[self.navigationController popViewControllerAnimated:YES];
}
And then setup part of the custom navigationItem as follows:
- (UINavigationItem*)navigationItem
{
UIBarButtonItem* newLeftBarButton = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(backButtonTapped)];
UINavigationItem* navigationItem = [[[UINavigationItem alloc] init] autorelease];
Hope this helps.
As shown in the screenshot below, i have a UITableView with some info and upon selecting a row an ABUnknownPersonViewController is invoked. In order to be able to able to dismiss that and go back to the UITableView I have this code:
ABUnknownPersonViewController *unknownPersonView = [[[ABUnknownPersonViewController alloc] init] autorelease];
[unknownPersonView setUnknownPersonViewDelegate:self];
[unknownPersonView setDisplayedPerson:personRecord];
[unknownPersonView setAllowsAddingToAddressBook:YES];
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"Επιστροφή" style:UIBarButtonItemStylePlain
target:self action:#selector(goBackToView)];
unknownPersonView.navigationItem.title = #"Προσθήκη στις επαφές";
unknownPersonView.navigationItem.leftBarButtonItem = anotherButton;
navigationController = [[[UINavigationController alloc] initWithRootViewController:unknownPersonView] autorelease];
//navigationController = [[[UINavigationController alloc] initWithRootViewController:self] autorelease];
//self.navigationItem.rightBarButtonItem = anotherButton;
[self presentModalViewController:navigationController animated:YES];
} // didSelectRowAtIndexPath ends here
- (IBAction)goBackToView {
[self dismissModalViewControllerAnimated:YES];
}
- (void)unknownPersonViewController:(ABUnknownPersonViewController *)unknownPersonView didResolveToPerson:(ABRecordRef)person {
// CallerIDAppDelegate *delegate = (CallerIDAppDelegate *)[[UIApplication sharedApplication] delegate];
[navigationController dismissModalViewControllerAnimated:YES];
}
The problem (as you can see) is that when the ABUnknownPersonViewController is dismissed by the "Επιστροφή" button, which is "Back" actually, the view holding the tableView and the blue UIButton is moved a couple of pixels to the bottom!
Any help on what could be causing this?
Screenshot http://dl.getdropbox.com/u/1237004/problem.jpg
Debug this by checking your view's frame in -viewWillAppear, -viewDidAppear, -viewWillDisappear, and -viewDidDisappear.
Also check the view's autoresizingMask, and the parent view's autoresizesSubviews property.
I'm not sure I see the value of setting up a navigation controller here. You could just present the ABUnknownPersonViewController with [self presentModalViewController: unknownPersonView];. If you're doing it for the sake of picking up the visual navigation bar with the back button, then just add a nav bar and button to the unknown person view.
It seems like a mixed metaphor to be creating a UINavigationController but then not using its usual navigation methods (e.g., pushViewController:animated: and popViewControllerAnimated:) and instead using the modal methods inherited from UIViewController.
It seems that adding this line:
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];
in my viewWillAppear: made the view not to move when the modal view controller is dismissed. However now the initial position was already slightly dislocated to the bottom but fixed it by moving all the outles in IB to the top so it looks ok.