what is the opposite of pushviewcontroller in iphone? - iphone

I m working on one project in which I m calling other viewcontroller in the below way:--
AppDelegate *app2 = (AppDelegate *)[[UIApplication sharedApplication]delegate];
[self.navigationController pushViewController:app2.SecondForm animated:NO];
app2 =nil;
My application is navigation based and in which I have created my on back button on navigation bar
-(void)viewDidLoad
{
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarStyleBlackOpaque target:self action:#selector(Back:)];
}
in this way my Back action will call on click
-(IBAction)Back:(id)sender
{
// What code I will write over here to get the same functionality
// which is by default given by navigation controller?
}

I think you are looking for:
[self.navigationController popViewControllerAnimated:YES];

popViewControllerAnimated

Related

Cannot add a right or left bar button item to my nav controller

Have tried all the examples on this website I just don't see anything on my modal view, I do see the navigationbar though but its empty
EditEntityViewController *editEntityViewController = [[EditEntityViewController alloc] init];
editEntityViewController.currentNode = newNode;
editEntityViewController.delegate = self;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:editEntityViewController];
navController.modalPresentationStyle = UIModalPresentationFormSheet;
navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"Show"
style:UIBarButtonItemStylePlain
target:self
action:#selector(refreshPropertyList:)];
editEntityViewController.navigationItem.rightBarButtonItem = anotherButton;
[anotherButton release];
[self presentModalViewController:navController animated:YES];
[editEntityViewController release];
As discussed, your code was correct and is the standard way to show a popup sheet with a UINavigationBar to hold buttons to dismiss the sheet. However, you had defined an IBOutlet in EditViewController called navigationItem, which was causing a conflict.
Try setting the rightBarButtonItem on editEntityViewController before you create the UINavigationController with initWithRootViewController:.
I think that the navigation bar is set up when the UINavigationController is created. Adding the right bar item after creation time is too late.
EDIT: Ok, so that's not the issue.
The following minimal code snippet works so I would check whether your EditEntityViewController is doing something to remove the button elsewhere:
- (IBAction)showPopup:(id)sender
{
UIViewController *popupController = [[UIViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:popupController];
navController.modalPresentationStyle = UIModalPresentationFormSheet;
navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"Show"
style:UIBarButtonItemStylePlain
target:self
action:nil];
popupController.navigationItem.rightBarButtonItem = anotherButton;
[anotherButton release];
[self presentModalViewController:navController animated:YES];
[popupController release];
}
The reason why this was not working is really stupid. Basically I had an IBOutlet defined in EditViewController called navigationItem which was conflicting with the SDK's property with the same name.
I removed it and the link from the nib and as Robin says it works perfectly.
Modally presented view controllers on navigation controllers don't have navigationItem nor navigationController properties. They DO, however, have parentViewController property, but this is irrelevant in your case.
If you want to customize navigation bar on your modally presented view, you should connect IBOutlet from view controller managing that view to the navigation bar placed in that managed view. Then do the manipulation through IBOutlet instance variable.

Display ModalViewController with button in MainWindow.xib

I'd like to display a ModalViewController from a bar button in the MainWindow.xib file. How would I do this? The basic code I'm looking to use is this:
-(IBAction)add {
myCustomViewController *add = [[myCustomViewController alloc] initWithNibName:#"myCustomViewController" bundle:nil];
[self presentModalViewController:add animated:YES];
[add release];
}
But where do I put it?
EDIT: I figured it out, in my navigation controller i put the following code in viewDidLoad:
UIBarButtonItem *addbutton = self.navigationItem.leftBarButtonItem;
[addbutton setTarget:self];
[addbutton setAction:#selector(add)];
and changed the function to:
- (void)add {
myCustomViewController *add = [[myCustomViewController alloc] initWithNibName:#"myCustomViewController" bundle:nil];
[self presentModalViewController:add animated:YES];
[add release];
}
Thanks for your help, Parth!
I fear that this is not possible.
You will have to put a viewController inside MainWindow.xib and put button on that viewController because you cannot add controls (like button in your case) on UIWindow.
It is required to be of type UIViewController or UITableViewController for you to be able to add UIControls to it.
Hope this helps you.

navigationbar title not getting set iphone

In my app, I'm presenting a modalviewcontroller as follows and I'm not able to change the navigationbar's title or any of its properties for that matter.
fullListTopCompanies *fullListTopCompaniesInstance = [[fullListTopCompanies alloc] initWithNibName:#"fullListTopCompanies" bundle:nil];
UINavigationController *cntrol = [[UINavigationController alloc] initWithRootViewController:fullListTopCompaniesInstance];
[fullListTopCompaniesInstance setTitle:#"TEST"];
UIBarButtonItem *submit = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(displayViewForPosts)];
fullListTopCompaniesInstance.navigationItem.rightBarButtonItem = submit;
[submit release];
[self presentModalViewController:cntrol animated:YES];
[cntrol release];
I tried instantiating application delegate and assigning its navigationcontroller to local navigationcontroller instance but no use.
Somehow that navigationcontroller is not accessible. It can't be accessed by using "self.navigationitem". Whenever I present modalviewcontroller with the navigationcontroller, this navigation comes below the actual navigationcontroller.
For Example, if you are trying to set title of navigation bar for the ViewController called "ABCViewController", then add
self.Title = #"";
in viewWillAppear Method of the ABCViewController and try to rebuild and Run.
Hope this helps. :)
Whenever I present modalviewcontroller with the navigationcontroller, this navigation comes below the actual navigationcontroller.
That problem is because calling presentModalViewController: on self, you should call it on self.navigationController that way the navigation controller won't be shown below the other one.
As to why you can't set the navigationController's properties, I don't know. It looks Ok to me. But I expect it is because you are setting the properties before viewDidLoad is called by the nib-loader. I think I remember having problems like this myself a long time ago.
You should set the title etc. in the UIViewController subclass's viewDidLoad method and I think you worries will be over.
I've created a simple view based app with xcode template, then i've added your code and it's working for me...
- (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.
TestViewController *fullListTopCompaniesInstance = [[TestViewController alloc] initWithNibName:#"TestViewController" bundle:nil];
UINavigationController *cntrol = [[UINavigationController alloc] initWithRootViewController:fullListTopCompaniesInstance];
[fullListTopCompaniesInstance setTitle:#"TEST"];
UIBarButtonItem *submit = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(displayViewForPosts)];
fullListTopCompaniesInstance.navigationItem.rightBarButtonItem = submit;
[window addSubview:viewController.view];
[window makeKeyAndVisible];
[viewController presentModalViewController:cntrol animated:YES];
[cntrol release];
[submit release];
return YES;
}

toolbarItems not being pushed with view

I have a NavigationController based iPhone app that has a navigationBar and a toolbar. Here is basically how it works:
The applicationDelegate pushes a "SplashScreen" onto the RootViewController as a modal view. While the splash screen is up, the application does some work and based on the user's location will either just dismiss the modal view OR will dismiss the modal view and push another view onto the navigation stack.
Both the RootViewController and the child view have toolbars with an Add button. My problem is this: when the 2nd view is pushed automatically, the Add button calls the code for its PARENT controller. If you dismiss this and then press the add button again, it calls the correct code.
Here is some of my code.
in the viewDidLoad of the RootViewController I have:
addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addClicked:)];
[self setToolbarItems:[NSArray arrayWithObjects:addButton, nil]];
in the viewDidLoad of the Child Controller (LocationListsController) I have:
addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addClicked:)];
[self setToolbarItems:[NSArray arrayWithObjects:addButton, nil]];
(yes the same code, they both have addClicked events)
in the RootViewController, viewWillAppear is where I actually push the child view:
if (((GeoListsAppDelegate *)[[UIApplication sharedApplication] delegate]).selectedIndex != -1)
{
GeoLocation *location = [((GeoListsAppDelegate *)[[UIApplication sharedApplication] delegate]).locations objectAtIndex:((GeoListsAppDelegate *)[[UIApplication sharedApplication] delegate]).selectedIndex];
((GeoListsAppDelegate *)[[UIApplication sharedApplication] delegate]).selectedIndex = -1;
if (lController == nil)
{
LocationListsController *aController = [[LocationListsController alloc] initWithLocation:location];
self.lController = aController;
[aController release];
}
[[self navigationController] pushViewController:lController animated:YES];
}
The pushing of the view works fine. The only problem I have is the addButton on the toolbar. Does anyone have any ideas?
Try pushing the "child" view controller from viewDidAppear instead of viewWillAppear.

Weird UI problem when modal view is dismissed

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.