can't dismiss presentModalViewController - iphone

i have two view controllers in iphone application.
1.) FirstVC
2.) SecondVC
In my FirstVC i have one button. By tapping on that button i opened SecondVC in presentModalViewController. look bellow code.
- (IBAction)buttonClicked:(id)sender
{
SecondVC *secondVC = [SecondVC alloc] initWithNibName:#"SecondVC" bundle:nil];
[self.navigationController presentModalViewController:secondVC animated:YES];
}
now moved to SecondVC. On SecondVC i have create navigation bar as well as "cancel" button as a leftBarButtonItem. i set a button clicked event on cancel button. in that method i want to dismiss SecondVC. bellow method is in SecondVC. look bellow code.
- (void)cancelButtonClicked
{
[self dismissModalViewControllerAnimated:YES];
}
This code dosen't work. i can't dismiss SecondVC by this code. Please suggest another tricks.

Change the button code to this..
- (IBAction)buttonClicked:(id)sender
{
SecondVC *secondVC = [SecondVC alloc] initWithNibName:#"SecondVC" bundle:nil];
[self presentViewController:secondVC animated:YES completion:NULL];
}
on cancelButtonClick
-(void)cancelButtonClicked {
[self.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
}

You are sending the dismissModalViewControllerAnimated: message to the wrong object.
Since you presented the modal view controller through the navigation controller, then you should call:
[self.presentingViewController dismissModalViewControllerAnimated:YES];
This will work on iOS 5 and newer. In case you are targeting only iOS 5 and newer, you could also think of using the newer methods that are available on it to manage modal view controllers:
– presentViewController:animated:completion:
– dismissViewControllerAnimated:completion:
but I don't think this is mandatory.
If you want to support iOS 4 and older, you should add a property to your modal view controller:
#interface SecondVC : UIViewController
#property (nonatomic, weak/assign) UIViewController* presentingController;
...
#end
and set it before displaying the controller modally:
- (IBAction)buttonClicked:(id)sender
{
SecondVC *secondVC = [SecondVC alloc] initWithNibName:#"SecondVC" bundle:nil];
secondVC.presentingController = self.navigationController;
[self.navigationController presentModalViewController:secondVC animated:YES];
}
then you would use:
[self.presentingController dismissModalViewControllerAnimated:YES];

try this for present secondVC...
[self presentModalViewController:secondVC animated:YES];

Please Replace your code with the following... code
- (IBAction)buttonClicked:(id)sender
{
SecondVC *secondVC = [SecondVC alloc] initWithNibName:#"SecondVC" bundle:nil];
[self presentModalViewController:secondVC animated:YES];
}
- (void)cancelButtonClicked
{
[self dismissModalViewControllerAnimated:YES];
}

Related

error when i am calling next view controller

i am getting an error when i am calling next view controller, i want to call a view controller that a action continue .this screen will come appear first time only when we run first time.
controller is not going next view.
-(void)viewDidLoad
{
welcomePage *temp = [[welcomePage alloc]initWithNibName:#"welcomePage" bundle:nil];
[self presentViewController:temp animated:YES completion:^(void){}];
}
Warning: Attempt to present on whose view is not in the window hierarchy!
Try this one..
You change the code in Appdelegate.m didFinishLaunchingWithOptions method like this
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:self.viewController];
self.window.rootViewController = nav;
And in your ViewDidLoad method like this..
welcomePage *temp = [[welcomePage alloc]initWithNibName:#"welcomePage" bundle:nil];
[self.navigationController presentViewController:temp animated:YES completion:nil];
For dismissing viewcontroller you can write following code in welcomepage button action
-(IBAction)dismiss:(id)sender{
[self dismissViewControllerAnimated:YES completion:nil];
}
use this code
-(void)viewDidLoad
{
welcomePage *temp = [[welcomePage alloc]initWithNibName:#"welcomePage" bundle:nil];
[self presentViewController:temp animated:YES completion:nil];
}
That error means you are trying to present a modal view controller from a view controller that is not currently in the screen.
If You are using Storyboard : Use this
instantiateViewControllerWithIdentifier
UIViewController objController = [self.storyboard instantiateViewControllerWithIdentifier:#"StoryBoard Id"];
[self presentViewController:objController animated:YES completion:nil];
To Set Storybaord ID , Check Here .
This error means that you are trying to present 'welcome page' from a view that is not on the screen yet.
Knowing that simply moving the code to the viewDidAppear method doesn't work for you, what you can try to do is this:
-(void)viewDidAppear
{
[self presentWelcome];
}
- presentWelcome {
welcomePage *temp = [[welcomePage alloc]initWithNibName:#"welcomePage" bundle:nil];
[self presentViewController:temp animated:YES completion:^(void){}];
}
What happens here is that when the view appears (so when it is in the view hierarchy), the controller starts a function that calls the other viewController. hope this solves your problem.
Hope this helps you!

iphone loss of tabbar during uimodaltransition

Okay, working on a transition between a view with a tabbar and another view which is to be an information/about view. I have the code to transition from the view with tabbar and to transition back to previous view, but during the transition back I lose the tabbar at the bottom. Not sure exactly how to approach this with the tabbar in the MainWindow.xib
E.g.:
(IBAction)backButtonPressed:(id)sender
{
TablesViewController *tvc = [[TablesViewController alloc] initWithNibName:#"TablesView" bundle:nil];
tvc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:tvc animated:YES];
[tvc release];
}
Thanks,
np
Try presenting the modal transition from the containing instance of UITabBarController and not the UIViewController the action was triggered from.
- (IBAction)backButtonPressed:(id)sender
{
TablesViewController *tvc = [[TablesViewController alloc] initWithNibName:#"TablesView" bundle:nil;
tvc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self.tabBarController presentModalViewController:tvc animated:YES];
[tvc release];
}
I had the exact same problem, did a ugly solution:
- (IBAction)backButtonPressed:(id)sender {
[self.navigationController dismissModalViewControllerAnimated:YES];
}

iPhone - Dismissing previous ModalViewControllerAnimated:YES then poping a new ModalViewControllerAnimated:YES - fail

I have a main window that pops a modal view controller. When done in this modal view controller, it returns to the main window then dismisses itself.
Then the main windows pops a new Modal view controller with animated=YES.
The problem is that the dismiss call that is done inside the first modalviewcontroller applies to both and SecondController is never shown.
Putting the first dismiss before or after the parent call does not change anything.
If first dismiss is set wih animate= NO, everything works fine. But I need the animation.
Main.m
- (void) entry {
FirstController *nextWindow = [[FirstController alloc] initWithNibName:#"theNIB" bundle:nil];
nextWindow.caller = self;
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:nextWindow];
[self.navigationController presentModalViewController:navController animated:YES];
[nextWindow release];
[navController release];
}
- (void) thingsDoneInFirstModalController:(OBJECT)returnValue retval2:(OBJECT2)returnValue2 {
[self display2ndController];
}
- (void) display2ndController {
SecondController *nextWindow;
nextWindow = [[SecondController alloc] initWithNibName:#"NIB2" bundle:nil];
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:nextWindow];
[self.navigationController presentModalViewController:navController animated:YES];
[navController release];
[nextWindow release];
}
1st ModalViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.navigationController dismissModalViewControllerAnimated:YES];
[self.caller thingsDoneInFirstModalController:theResult retval2:someMoreResult];
}
What may I do ?
I don't want to catch anything in view....Disapear...
Why does the dismiss colides as they are not called from the same navigation controller ?
The reason is likely the animation when you dismiss it. Try showing the second modal window using the performSelector:withObject:afterDelay:, a method inherited from NSObject. Reference here.

Problem with info view on iPhone app

I am currently using this code to bring up an info view for an iPhone app.
-(IBAction)showInfo:(id)sender {
InfoView *info = [[InfoView alloc] initWithNibName:nil bundle:[NSBundle mainBundle]];
info.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentModalViewController:info animated:YES];
[info release];
}
And this code go back to my main view
-(IBAction) exitInfo:(id)sender {
[self.parentViewController dismissModalViewControllerAnimated: YES];
}
When I run it on the simulator, it goes fine, however, on the actual iPhone, when I press the button that triggers the exitInfo method, it carries out its animation, but once it is completely off the screen and only my main view is visible, the screen will flicker, quickly showing the info view again. How can I fix this?
In my opinion, your viewController shouldn't even know it is modally viewed.
Try putting the code that send the dismissModalViewController message in your main controller and alert your main controller that the user clicked the dismiss button using a delegate.
something like this in your "parent" viewController
-(IBAction)showInfo:(id)sender {
InfoView *info = [[InfoView alloc] initWithNibName:nil bundle:[NSBundle mainBundle]];
info.modalTransitionStyle = UIModalTransitionStylePartialCurl;
info.delegate = self;
[self presentModalViewController:info animated:YES];
[info release];
}
-(IBAction)closedButtonClicked {
[self dismissModalViewControllerAnimated: YES];
}
and in your "Modal" viewController
-(IBAction) exitInfo:(id)sender {
[delegate closedButtonClicked];
}
Of course you will need a protocol like
#protocol MyCustomDelegate
-(IBAction)closedButtonClicked;
#end
and something like this in your "modal" interface
-id<MyCustomDelegate> delegate;

presentModalViewController not working

here's my code:
ViewController *vc = [[ViewController alloc] initWithNibName:#"TableView" bundle:nil];
[self.navigationController presentModalViewController:vc animated:YES];
//[self setView:[vc view]];
If I call it, nothing happens. However, if I change it to:
ViewController *vc = [[ViewController alloc] initWithNibName:#"TableView" bundle:nil];
//[self.navigationController presentModalViewController:vc animated:YES];
[self setView:[vc view]];
The view appears just fine (without the transition, of course).
What am I doing wrong? Is there anything special you have to take care of when initializing the view controller? I tried to copy as much as possible from Apple's examples, but I can't get this to work...
Thanks for any input!
-- Ry
You can only present modal view controllers from controllers that have already been shown onscreen (usually through a UINavigationController or UITabBarController). Try creating a UINavigationController, pushing a viewController to it, and then presenting your modal controller. There's a starter project in Xcode that shows how to create a UINavigationController-based flow if you're unfamiliar with it.
One other thing to note: if you haven't pushed the view controller onto a UINavigationController, the .navigationController property will be nil, and messaging it will have no effect.
I encountered the same problem while attempting to show a modal view over another modal view. Ben's answer is correct, and can be implemented like so:
#interface FirstView: UIViewController {
UIViewController *firstView;
}
- (IBAction)showOptionsView:(id)sender;
#end
In the main view class:
- (void)viewDidLoad {
[super viewDidLoad];
firstView = [[UIViewController alloc]init];
[firstView setView:self.view];
[firstView setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
}
- (IBAction)showOptionsView:(id)sender {
OptionsView *optView = [[OptionsView alloc]initWithNibName:#"OptionsView" bundle:nil];
if(firstView != nil) {
[firstView presentModalViewController:optView animated:YES];
[optView release];
}