How to implement a Modal View in a TabBar iOS app - objective-c

I am trying to present a modal view in a tabbar app. I am using the code
- (IBAction)newView
{
[self.viewController presentModalViewController:viewController
animated:YES];
}
linked to a button. When the button is pressed, nothing happens and nothing is displayed on the log. This is most likely simple to fix, but I have not found anything that has worked yet.
Thanks

Use this and you rock:
[self presentModalViewController:viewController animated:YES];

Related

navigationController popToRootViewControllerAnimated not working in iOS 8

I have a login UIViewController embedded in a UINavigationController that a user enters UserID/Password then clicks [Login] button looks like this:
2-textfields and a UIButton, when the user clicks the button I run the code:
-(void)btLoginTapped:(id)sender{
[doLogin];
NSLog(#"Login Successful.")
[self.navigationController popToRootViewControllerAnimated:YES];
}
now my problem arises when I use Simulator or a Device with iOS 8
I will see the Log-message but the ViewController will not pop where as when I use a Simulator/Device with iOS 7 the view will pop to root as normal.
why won't
[self.navigationController popToRootViewControllerAnimated:YES]
work in iOS 8+ but it'll work in iOS 7+?
Mind you I have changed the Animated flag to NO, I also tried:
[self.navigationController popViewControllerAnimated:YES/NO] but still same issue?!!
Any Input is appreciated. Thank you.
Try this
[self dismissViewControllerAnimated:YES completion:nil];
instead of popToRootViewControllerAnimated if you can push your root view controller then it will work.
Hope it helps.

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].

Application tried to present modally an active controller : UIImagePickerController

I'm struggle at this for 2 days and believe that this is the moment I should call for help. After I search SOF for a while, none of any answer could solve my problem. Here are my application ...
In the application,
Device is iPad, iOS 6
RootViewController is NavigationController
TopViewController is TabBarController
In this TabBarController, I present a popoverController from right bar button of navigation bar
In presenting popover there is a button to allow user to pick image from by taking new one or pick from existing.
To pick new one, I presentViewController UIImagePickerController to allow user to take photo with divice camera. presentModalViewController:animated: if iOS < 6, and presentViewController:animated:completion: for iOS > 6
I also hide Status Bar before presentation
To select from existing photo, I do presentPopoverFromBarButtonItem:permitArrowDirections:animated:
PopoverViewController also referencing by A TabBarController
Here is the issue
Present UIImagePickerController will always failed if user try to pick new one first with exception "Application tried to present modally an active controller <[name of view controller that try to present]>"
BUT, if user try to pick image from camera roll for once and then try to take new one again, it won't fail.
Here are what I tried
present from RootViewController
present from TopViewController (TabBarController)
present from popoverViewController itself
present from a tab of TabBarController
hide popoverViewController before presentation
resignFirstResponder from a textField in popoverViewController
Here is the current code I'm using
// PopoverViewController, presented by a tab in TabBarController
- (IBAction)takePhoto:(id)sender {
[self.delegate takePhotoWithDeviceCamera];
}
// A Tab in TabBarController, delegate of popoverViewController
- (void)takePhotoWithCamera {
[[UIApplication sharedApplication] setStatusBarHidden:YES];
if ([UIDevice OSVersion] < 6.0) {
[self presentModalViewController:cameraPicker animated:YES];
} else {
[self presentViewController:cameraPicker animated:YES completion:nil];
}
}
Any idea what would cause this error? Any suggestion are welcome. Thank you.
Got the same trouble than you and finally got the solution based on #CainaSouza's answer. I've been working with Xamarin.iOS so I'll make my answer in C#, but it can be easily translated to Objective-C.
I'm using the same code as #CainaSouza to call the controller:
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController (customController, true, null);
And then I add the following code to my custom RootViewController:
public override void PresentViewController (UIViewController viewControllerToPresent, bool animated, Action completionHandler)
{
if (PresentedViewController != viewControllerToPresent) {
base.PresentViewController (viewControllerToPresent, animated, completionHandler);
}
}
The trick is to check if you haven't presented that UIViewController before.
I know it's an old question, but hope it will help someone. :)
Present the imagePicker controller in a popoverController(in case of iPad). This will not give you that error.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:picker];
[popover presentPopoverFromRect:self.selectedImageView.bounds inView:self.selectedImageView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
self.popOver = popover;
}
else {
[self presentModalViewController:picker animated:YES];
}
Best Regards.
Have you tried to present it like this?
[self.view.window.rootViewController presentModalViewController:cameraPicker animated:YES];
My guess is that the cameraPicker instance is not correctly allocated/released. Try creating the cameraPicker inside your - (void)takePhotoWithCamera method rather than relying on a previously created instance. You'll get a handle to the picker instance in the callback methods...
I had the same problem - I wanted users to take photos using a full screen view (i.e. call presentViewController and pass UIImagePickerController controller instance) and select existing photos from a popover (I associated it with a popover using initWithContentViewController). I reused the same instance of UIImagePickerController for both camera and popover and it threw the same exception if I tried to run a camera before opening a popover.
I turned out to cause a problem and my solution was simply to have two instances of UIImagePickerController - one for camera (which I presented from a main view) and another one for popover. It works so far. :-)
Not sure if it is still actual for the original poster, but hopefully it will help anyone else who encounter this discussion.

Closing a view displayed via a modal segue

I'm manually invoking a segue (set as modal) in order to display a login form in Xcode 4.2 using Storyboards with the following line of code:
[self performSegueWithIdentifier:#"LoginSegue" sender:nil];
I'm probably missing something really simple, however I can't find a way to programmatically close the login view and return to the previous view.
The view is part of a navigation view controller, so setting the segue type to "push" allows me to use the back button to send me back to my previous screen, but in "modal" mode, I'm not entirely sure how to achieve this (after button press, for example)
Any help would be much appreciated.
If your deployment target is iOS 5.0 or later, use this message:
[self dismissViewControllerAnimated:YES completion:nil];
Or in Swift:
self.dismissViewControllerAnimated(true, completion: nil)
If your deployment target is older, use this (deprecated) message:
[self dismissModalViewControllerAnimated:YES];
[self dismissViewControllerAnimated:YES completion:nil]; is a new way in IOS5
The following should work fine...
[self dismissModalViewControllerAnimated:YES];
I do exactly this with a login page in my latest tutorial here, with no issues.
The following code works in swift 3:
self.dismiss(animated: true, completion: nil)

Hiding the toolbar

I am doing application using ios 4.0. I need to hide UIToolbar when I go from second view to firstview. As secondview consists UIToolbar. In secondView I am having NavigationBarButton I wrote the code as below.
-(void)back
{
[toolbar removeFromSuperview];
[self.navigationController popToRootViewControllerAnimated:YES];
}
So, I am able to hide when I am coming from firstview to secondview but the problem is when I am in secondview if I click on home button and again if run the application then its starts from secondview as I stoped there. Now, if I pop to firstview I am getting toolbar. I am not getting what's the reason and problem.
Can any one help me to solve this.Thanks in advance.
Have you try this when hiding toolbar?
[self.navigationController setToolbarHidden:YES animated:NO];