viewController management programmatically - objective-c

I have a problem about viewController. I created a program What is viewController based applicaiton. There is 4 button on mainViewController. I used this code for calling mainviewController
-(void) applicationDidFinishLaunching:(UIApplication *)application{
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
}
Then click to any button on homePage , I go to the other viewController. This code for call another viewController belong
-(IBAction)clickCalendarButton{
calendarButton.selected=YES;
[calendarButton
setImage:[UIImage imageNamed:#"afvalkalender_pressed.png"] forState:(UIControlStateHighlighted+UIControlStateSelected)];
GarbageCalendar *garbageCalendar = [[GarbageCalendar alloc] initWithNibName:#"GarbageCalendar" bundle:nil];
[self presentModalViewController:garbageCalendar animated:YES];
}
And then I want to go home page from another viewController. But I didn' go home page viewController.

Create button on detail view controller, which calls something like this:
- (IBAction)goBack {
[self dismissModalViewControllerAnimated:YES];
}

If you want to keep your current UI design, based on modal view controllers, then I think you should ensure that your other view controllers have got a button that does the dismiss of the view. Say, e.g., a "Back" or "Done" button. When you click on that button, a delegate method is called that executes: [self dismissModalViewControllerAnimated:YES];
Look also at this document for more info, section "Dismissing a Modal View Controller".
If you would like to consider alternative approaches to your UI, you could look into using a UINavigationController, which would make your life a little bit easier with navigating back from one controller to another.

Related

iOS7 - popToRootViewControllerAnimated not doing anything

I have looked around but haven't found a satisfying answer. My problem is that whenever I call popToRootViewControllerAnimated:(BOOL) it is not doing anything. When I NSLog it, it logs (null).
Let me back up a bit here. I have a table view controller that has a list of things, at the navigation bar up top there is an option to add and that takes me to a new view controller with a segue "Present as PopOver" which gets rid of the principal or main navigation bar. So I made one manually and added 2 bar button items "Cancel" and "Add". When "Cancel" is tapped, it should take the user back to the table view controller and discard changes, when "Add" button is tapped, it should also take user back to the previous table view controller with the changes. But it's not doing anything.
Here is my code.
- (IBAction)cancelButton:(UIBarButtonItem *)sender {
UINavigationController * navigationController = self.navigationController;
NSLog(#"%#", navigationController);
NSLog(#"cancel tapped though");
ListingTableViewController *rootController = [[ListingTableViewController alloc] init];
[navigationController popToRootViewControllerAnimated:NO];
[navigationController pushViewController:rootController animated:YES];
}
As far as the segue, this view controller is not connected to anything, or should I connect it? This is a noobish question indeed. Here is my xcode screenshot.
Check this link for the screenshot of the storyboard
http://i.stack.imgur.com/lqnCF.png
You must call
- (IBAction)cancelButton:(UIBarButtonItem *)sender {
NSLog(#"cancel tapped though");
[self dismissViewControllerAnimated:YES completion:nil];
}
instead of popToRootViewControllerAnimated because your VC presented and not pushed!
When presenting a view, you are not pushing it in your navigation controller, but having it presented. To dismiss it, try using [self.presentingViewController dismissViewControllerAnimated:NO completion:nil].

What is the design pattern for navigating between ViewControllers?

I currently have 1 storyboard which contains 2 view controllers: ViewController and TableViewController. The ViewController is the login view, and the TableViewController is the page that displays results (results view).
Currently, I did not create a segue from the login view to the results view. Instead, on the login view, after a user presses the login button and is authenticated, I programmatically push to the results view as follows.
XYZResultsTableViewController* controller = [[XYZResultsTableViewController alloc]init];
UINavigationController *navController = self.navigationController;
[navController popViewControllerAnimated:NO];
[navController pushViewController:controller animated:YES];
Indeed, the results view shows, but there is a "< Back" button at the top left, which, if pressed, goes back to the login view.
So, my question are:
How do I get rid of the login view from the view stack? (so the back button on the results view does not show)
Is this "programmatic" way of navigating between views "bad"? Meaning, should I rely on the storyboard and segues instead? Should I navigate to a new storyboard (I've seen this question asked on SO, though I haven't visited it yet)?
I'm a newcomer, so any help is appreciated.
If you don't want to use the navigation stack, you have to use presentViewController instead of pushViewController
XYZResultsTableViewController* controller = [[XYZResultsTableViewController alloc]init];
[viewController1 presentViewController:controller animated:YES];//viewcontroller1 is current view controller
Never use the code below unless you want to have the navigationController stack in viewController you are showing
/*XYZResultsTableViewController* controller = [[XYZResultsTableViewController alloc]init];
UINavigationController *navController = self.navigationController;
[navController popViewControllerAnimated:NO];
[navController pushViewController:controller animated:YES]; */
for more information on this difference between presentViewController and UINavigationController?
http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html

navigating from popup to popup in objective c

I am working with Objective C using xcode and SUP 2.1.3 as backend.I am verymuch new to the technology.I have created a project with master detail as the design template.In that in the detail view I am using a popup to display some details.And also I have a button over there. When I click in this button I have to go to the next UIView in popup itself.
For that I have created two more UIViewControllers (viewController1 and viewController2).
And in detailview I have written the code for a popup like,
-(void)popupAgain
{
ViewController1 *viewController = [[ViewController1 alloc] initWithNibName:#"ViewController1" bundle:nil];
viewController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:viewController animated:YES];
[viewController release];
}
And I put a button over their in that popup, I am calling the next popupAgain funcion to get viewController2 like
-(IBAction)next:(id)sender
{
[self popupAgain];
}
-(void)popupAgain
{
ViewController2 *viewController_new = [[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil];
viewController_new.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:viewController animated:YES];
[viewController_new release];
}
Now the problem is when I click on the next button I have to dismiss the first popup and display the second.But I am not able to dissmiss the first one even if I am writing the code [self dissmissModalViewControllerAnimated:YES]; in the action for next button like,
-(IBAction)next:(id)sender
{
[self dissmissModalViewControllerAnimated:YES];
[self popupAgain];
}
Please anyone help me to solve this issue.Or do you have any other idea regarding this?I am very much new to the technology.Thank you very much for any help in advance.
See here:
http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html#//apple_ref/doc/uid/TP40007457-CH111-SW1
Dismissing a Presented View Controller
When it comes time to dismiss a presented view controller, the preferred approach is to let the presenting view controller dismiss it. In other words, whenever possible, the same view controller that presented the view controller should also take responsibility for dismissing it. Although there are several techniques for notifying the presenting view controller that its presented view controller should be dismissed, the preferred technique is delegation. For more information, see “Using Delegation to Communicate with Other Controllers.”

How to presentModalViewController without dismiss the TabBarController

Hey guys i`m trying to present a modal view controller inside an application with a tab bar controller. The problem is, every time the new view is presented, it on top of my tab bar.
I need to keep my tab bar even when the view is presented. Like google maps application does with his toolbar at the bottom of the screen.
How can i do that?
Thank you
By default, a modal view controller is meant to take up the entire screen (on an iPhone/iPod, at least). Because of this, it covers whatever you have on screen at the time.
A view controller presented via modal segue is meant to live on its own. If you want to keep your Navigation and TabBar, then just use a push segue to present the new ViewController. Remember to use this kind of segue, your presenting controller needs to be part of a UINavigationController already.
Use this to push a ViewController. If it is a UINavigationController it will push its linked RootViewController by itsself.
Create a viewController to push: (Using Storyboard)
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
or (Using Code/Nibs)
LoginViewController *viewController = [[LoginViewController alloc] init]; //initWithNibNamed in case you are using nibs.
//in case you want to start a new Navigation: UINavigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
and push with:
[self.navigationController pushViewController:vc animated:true];
Also, if you are using Storyboards for the segues you can use this to do all the stuff. Remember to set the segue identifier.
[self performSegueWithIdentifier:#"pushLoginViewController" sender:self]; //Segue needs to exist and to be linked with the performing controller. Only use this if you need to trigger the segue with coder rather than an interface object.
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"pushLiftDetail"]) {
[[segue.destinationViewController someMethod:]];
segue.destinationViewController.someProperty = x;
}
}
I think you'll need to add a UITabBar to the modal view and implement/duplicate the buttons and functionality that your main bar has. The essence of a modal window is it has total control until it is dismissed.
You might try putting your UITabBarController into a NavBarController, but I'm not certain that this will work.
UITabBarController -> NavBarController -> Modal View

Modal UIViewController will not push to next UIViewController

The start of the structure is as follows...
UITabBarController -> UINavigationController(s)
From each of the UINavigationControllers, I have a UIBarButtonItem that modally presents a UIViewController.
This UIViewController has a MKMapView with pins at multiple locations. When clicked, they display an annotation with a disclosure button.
Within this UIViewController, it is my intention to push a detail page (UITableViewController) when pressing the disclosure button of the annotation. The method calloutAccessoryControlTapped: receives the appropriate pin, but the transition to the next controller fails.
I have tried every combination of the following methods...
[self.navigationController ...]
[self.parentViewController ...]
[self.parentViewController.navigationController ...]
with the method being either...
presentModalViewController:
pushViewController:
I have done all of these with the UIViewController being on its own, and also with it embedded inside of a UINavigationController.
All of these properties return null...
self.navigationController
self.parentViewController
self.parentViewController.navigationController
This is the first time I've used storyboard for an Xcode project. Am I missing a step?
Try getting rid of the code and implementing the transitions in storyboard by control dragging from the button to the view controller you wish to load modally. When the "Storyboard Segue" menu pops up select "modal". In the modal view controller, I like to use code to return from the modal by calling:
[self dismissModalViewControllerAnimated:YES];
To Presenting Storyboard View Controllers Programmatically scroll to that section in gravityjack on the link provided.
For example, I have a view controller that I created in storyboard which I can call programmatically with the following two statements:
SettingsViewController *settingsVC = [self.storyboard instantiateViewControllerWithIdentifier:#"settingsVC"];
[self.navigationController pushViewController:settingsVC animated:YES];