Kill View and back to rootViewController - iphone

I am new to IOS, sorry in advance if I ask a stupid question.
I use UITabBarController and navigationController to control view.
At my last view, I would like to have a button when the button is pressed, view will return to rootViewController which I set by MainWindow.xib file and kill any process which run in app background.
this is my code in the last view before I want to back to rootViewController:
-(IBAction)doneButtonPressed:(id)sender{
JourneyIndexViewController *journeyIndexVC = [[JourneyIndexViewController alloc] initWithNibName:#"JourneyIndexViewController" bundle:nil];
[journeyIndexVC setDistanceLabelValue:self.distanceLabelValue];
[self.navigationController pushViewController:journeyIndexVC animated:YES];
[journeyIndexVC release];
[self dismissModalViewControllerAnimated:YES];
}
JourneyIndexViewController is the rootViewController that I set in MainWindow.xib.
Thank you very much for your advance support.

try
[self.navigationController popToRootViewControllerAnimated:YES];
You should take a look at this: UINavigationController Class Reference for better understanding

I am making a few assumptions here, but if JourneyIndexRootViewController is your rootViewController and is created in IB (in a nib), you do not need to re-crete it when pushing the button. It sounds like you simply need to remove the UINavigationController that you added on top of the rootViewController.

Try this. This should pop you back to the Previous View Controller.
[self.navigationController popViewControllerAnimated:NO];
Hope this helps

Related

Open ViewController on button click

I am new to iOS, can any one please help me to open and activity on Button click.
I have tried below methods, but the app remains on same ViewController, i am using Singleview Application type
Second *sec = [[Second alloc] init];
[self.navigationController pushViewController:sec animated:YES];
Second *sec = [self.storyboard instantiateViewControllerWithIdentifier:#"SecondV"];
[self.navigationController pushViewController:sec animated:YES];
can anyone please help me to solve this.
if you are using storyboard then Embed your viewcontroller to UINavigationController Editor->EmbedIn->NavigationController try this
If not using Storyboard, then following might help:
Second *sec = [[Second alloc] initWithNibName:#"Second" bundle:nil];
[self.navigationController pushViewController:sec animated:YES];
For Storyboard enabled code:
Second *sec = [self.storyboard instantiateViewControllerWithIdentifier:#"YourIdentifierForVC"];
[self.navigationController pushViewController:sec animated:YES];
BTW, I strongly suggest you to google the problem for solution & search SatckOverflow - first, before posting as a new question.
Also, you should reconsider the naming for the class. Just a suggestion.
to push to another view controller you need to set a UINavigationController in front of your UIViewController in storyboard. Your self.navigationController might be nil, that's why it is not pushing your view controller.
Therefore drag and drop a Navigation Controller to your storyboard and delete the default TableViewController (make sure to click on empty space in storyboard before selecting and deleting it) and connect the UINavigationController to your own 'single view controller' by right-clicking on the yellow arrow and connecting the rootViewController outlet with it).
Then, select the UINavigationController on your storyboard and in Attributes Inspector (the fourth symbol in the right panel) under the section View Controller select Is Initial View Controller.

Best way to switch View Controller in iOS

I have 2 view controllers in my project. Inside View Controller1 I want to switch to View Controller 2 by press of a button. Currently I do this
- (IBAction)startController2:(id)sender {
viewController1 vc2 = [[viewController2 alloc] init];
self.view = vc2.view;
}
This seems to work fine, but there is a big delay (4 secs) between the button press and second view controller appears. If I call the viewController2 directly from the AppDelegate things load faster. What am I doing wrong here. Any help is greatly appreciated.
Several things to consider.
Part 1: "What am I doing wrong here"?
You definitely didn't mean to do self.view = vc2.view. You just put one view controller in charge of another view controller's view. What you probably mean to say was [self.view addSubview:vc2.view]. This alone might fix your problem, BUT...
Don't actually use that solution. Even though it's almost directly from the samples in some popular iPhone programming books, it's a bad idea. Read "Abusing UIViewControllers" to understand why.
Part 2: What you should be doing
It's all in the chapter "Presenting View Controllers from Other View Controllers".
It'll come down to either:
a UINavigationController, (see the excellent Apple guide to them here) and then you simply [navigationController pushViewController:vc2]
a "manually managed" stack of modal view controllers, as andoabhay suggests
explicitly adding a VC as child of another, as jason suggests
You should consider using UINavigationController to switch view controllers. If your building target is iOS 5.0+, you can also use the new controller container concept: [mainViewController addChildViewController:childViewController].
Use presentModalViewController as follows
[self presentModalViewController:vc2 animated:YES completion:^(void){}];
and in the viewController1 use
[self dismissModalViewControllerAnimated:YES completion:^(void){}];
where ever you want to go back to previous controller.
[aController presentViewController:bController animated:NO completion:nil];
[bController presentViewController:cController animated:NO completion:nil];
when you want dismiss cController, you can do like this
[aController dismissViewControllerAnimated:NO completion:nil];
this is the flow chart.
aController → bController → cController
↑___________________________↓
You should use UINavigationController to switch view controllers.
You are on View1 and add the following code on button click method.
View2 *View2Controller = [[View2 alloc] initWithNibName:#"View2" bundle:nil];
[self.navigationController pushViewController:view2Controller animated:YES];

Issues with the UIViewController and UINavigationController

Ok, i need help for this action in Table
You can either use -popToRootViewControllerAnimated: or -popToViewControllerAnimated:
In your case for returning to view 1, use:
[self.navigationController popToRootViewControllerAnimated:YES];
If you want to return to a specific view controller, get it's reference and then do:
[self.navigationController popToViewController: specificViewController animated:YES];
If you are using Storyboard...
Create a Segue pointing to View1 VC and perform that Segue when the Back button is triggered.
If not, Sid's answer is correct.

Dismiss ModalViewController from another viewController in subview

I've got a view called A open with presentModalViewController Method, inside this view I loaded secondary view using:
new_view = [[new_websongs alloc] initWithNibName:#"new_websongs" bundle:nil];
[mysubview addSubview:new_view.view];
ok, to here it's ok but now I need to dismiss the first view "A" calling a method [self dismissModalViewControllerAnimated:YES] situated if first "A" viewController from secondary view controller (new_view) but not work! the code is:
self.Aviewcontroller = [[Aview alloc] init];
[Aviewcontroller dismissModalViewControllerAnimated:YES];
[Aviewcontroller release];
Please help ME!!!!
Thanks
Did u try [self.parentViewController dismissModalViewControllerAnimated:YES];
You have a logical problem. Start reading View Controller Programming Guide for iOS
The view controller that present an modal view controller must dismiss it or the modal view controller must dismiss it self
Totally agree with other answers; think logically about the order of view controller order and type. So think about which controllers are shown modally, and those shown via a navigation controller.
You can of course set a number of view controllers with:
- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated
without animation, then when required call say:
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
to show a specified view controller further up your stack of view controllers.
Hope this helps think about what you need to do? It's often a good idea to think about the order and type of view controllers in your app's interface in a separate project - where you can try it out on the device itself.
try this it should work
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
This works if you are presenting a modal view from a UISplitViewController. It can also be applied in so many other ways...
First, create an instance in your .h file for your appDelegate, (AppDelegate_iPad *appDelegate) then put this in your viewDidLoad or comparable method:
ipadDelegate = (AppDelegate_iPad *)[[UIApplication sharedApplication] delegate];
Now, present the first modal view like this:
YOURVC *vc = [[YOURVC alloc] initWithNibName:#"YOURVC" bundle:nil];
[ipadDelegate.splitViewController presentModalViewController:vc animated:YES];
[vc release];
Say you have a subview, like a UITableView, and want to dismiss the modal from the didSelectRowAtIndexPath. All you have to do to dismiss your modal with a subview is create another ipadDelegate instance inside your subview's .h (if needed), reference the [[UIApplication sharedApplication] delegate] again, and dismiss:
[ipadAppDelegate.splitViewController dismissModalViewControllerAnimated:YES];
Essentially, as long-winded as it may be, use your appDelegate's controller to present and dismiss the the modal if you need to maintain a persistent reference to the presentingViewController...because all the things above just don't work in my case.
If you're presenting with your ipadDelegate, make sure you check the mode of presentation in your MainWindow_iPad.xib. Your "Transition Style" should be "Cover Vertical" and "Presentation" should be "Current Context" or your modal may present behind other views.

Showing a modal view controller from a tab bar app

First, I would like to warn that I am a complete newbie into iPhone coding...
I need to show up a viewcontroller from a library, I know that it is modal. I have a tab bar app (created with the default XCode template). I need to show that viewcontroller, there are no problem if it hides the tabbar itself... But I am quite clueless, I don't know even what to search, or what to read...
You can call presentModalViewController:animated: to display another UIViewController modally.
EDIT: If you want to display your modal view in response to a button touch (for example), you would display it like this:
- (IBAction)buttonTouched:(id)sender
{
ModalViewController* controller = [[ModalViewController alloc] init];
[self presentModalViewController:controller animated:YES];
[controller release];
}
Then when you want to dismiss the modal controller, call dismissModalViewControllerAnimated:. This can be called either on your main view controller, or the modal one.
I don't know even what to search, or
what to read...
View Controller Programming Guide is a good place to start to help you understand view controllers (including modal ones). If that's confusing, get a bigger picture with iOS Application Programming Guide or start at the very beginning.
You can call modal view as
YourViewController *yvc = [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:YES]
[self presentModalViewController:yvc animated:YES];
You can call it in the IBAction method in case you want to call it on any control event like Button Click
-(IBAction)buttonClicked:(id)sender
{
YourViewController *yvc = [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:YES]
[self presentModalViewController:yvc animated:YES];
}
You can call it using self.
Hope this helps you.
If you have more doubts on this then you can ask me.