I'm doing an ios project without using any story board and xibs.
Here I've come out one problem on presenting my UIViewController.
Here is the senario:
My RootViewController has 2 UIButton's that can present ViewControllerA and ViewControllerB when being pressed.
-(IBAction)btnAclicked:(id)sender{
[self.navigationController presentViewController:ViewControllerA];
}
-(IBAction)btnBclicked:(id)sender{
[self.navigationController presentViewController:ViewControllerB];
}
And now inside ViewControllerA
I have a button to presentViewContollerB as well, and I want only the RootViewController to be displayed when I dismiss ViewContollerB.
In order to do this, I need to dismiss ViewControllerA first then presentViewControllerB. I know there is ways like using delegate to make it works, but I just want to know if there is any easier way to do this.
To be emphasised is that I want to use presentViewController only, not pushViewController. Thanks
I have an answer for your question,just do the following code,
in RootViewController.m
- (IBAction)gotoViewA:(id)sender
{
ViewControllerA *viewControllerA = [[ViewControllerA alloc]initWithNibName:#"ViewControllerA" bundle:nil];
[self presentViewController:viewControllerA animated:YES completion:nil];
}
- (IBAction)gotoViewB:(id)sender
{
ViewControllerB *viewControllerB = [[ViewControllerB alloc]initWithNibName:#"ViewControllerB" bundle:nil];
[self presentViewController:viewControllerB animated:YES completion:nil];
}
in ViewControllerA.m
- (IBAction)actionBackFromA:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
in ViewControllerB.m
- (IBAction)actionBackFromB:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
Use the completion block of presentviewcontroller,viz will executed first
[self presentViewController:goTo_B animated:YES completion:^{
//Dismiss A
[self dismissViewControllerAnimated:YES completion:nil];
}];
If this does not work,write dismiss and just after it present your new controller
[self dismissViewControllerAnimated:YES completion:nil];
[self.view.window.rootViewController presentViewController:goTo_B animated:YES completion:^{ }];
There's another way out, but i don't know whether its appropriate or not.
Create a property of type View Controller A in view controller B.
While presenting view controller B, assign an instance of View Controller A to the property in view Controller B.
[self presentViewController:goTo_B animated:YES completion:^{
goTo_B.propertyOfViewControllerA = self;
}];
After that while dismissing view controller B you can do the either of the two:
[self dismissViewControllerAnimated:YES completion:nil];
[propertyOfViewControllerA dismissViewControllerAnimated:YES completion:nil]
or
[self dismissViewControllerAnimated:YES completion:^{
[propertyOfViewControllerA dismissViewControllerAnimated:YES completion:nil]
}];
Related
On my project we have three windows A, B and C. From A I would like to push view B, and from B I would like to present view C.
My code:
ViewController A:
ViewControllerB *vcB = [[viewControllerB alloc]
initWithNibName:#"ViewControllerB" bundle:nil];
[[self navigationController] pushViewController:vcB animated:YES];
View Controller B:
ViewControllerC *vcC = [[ViewControllerC alloc]
initWithNibName:#"ViewControllerC" bundle:nil];
[self presentViewController:vcC animated: true completion: nil];
Everything is ok until now, but when I dismiss the last view controller with:
[[self presentingViewController] dismissViewControllerAnimated:NO completion:nil];
The app goes back to first view controller (vcA) instead the second one (vcB)
What am I doing wrong?
Thank you, guys.
You must be doing something else that you're not telling us about...
This works as expected:
In MyFirstViewController.m
- (IBAction)pushTapped:(id)sender {
MyPushedViewController *vc = [[MyPushedViewController alloc] initWithNibName:#"MyPushedViewController" bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
}
In MyPushedViewController.m
- (IBAction)presentTapped:(id)sender {
MyPresentedViewController *vc = [[MyPresentedViewController alloc] initWithNibName:#"MyPresentedViewController" bundle:nil];
[self presentViewController:vc animated:YES completion:nil];
}
In MyPresentedViewController.m
- (IBAction)dismissTapped:(id)sender {
[self dismissViewControllerAnimated:NO completion:nil];
}
Tapping the "dismiss" button in MyPresentedViewController dismisses the presented view controller (your vcC), leaving me at MyPushedViewController (your vcB) ... NOT at MyFirstViewController (your vcA).
try this to dismiss vc
[self dismissViewControllerAnimated:NO completion:nil]
and use this to pop
[self.navigationController popToViewController:controller animated:YES];
[self presentingViewController] = Bvc , so here you actually dismissing B not C
[Bvc dismissViewControllerAnimated:NO completion:nil];
but self = Cvc so here you dismissing C only
[self dismissViewControllerAnimated:NO completion:nil];
I have a UIViewControllerC which is call from two another UIViewcontroller
ViewControllerA
ViewControllerB
From ViewControllerA to go to UIViewControllerC i have to make it presentviewcontroller
UIViewControllerC *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"UIViewControllerC"];
[vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
vc.tagfrom=#"present";
[self presentViewController:vc animated:NO completion:nil];
From ViewControllerB to go to UIViewControllerC i have to make it push view
UIViewControllerC *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"UIViewControllerC"];
[vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
vc.tagfrom=#"push";
[self.navigationController pushViewController:vc animated:YES];
Now i have to back from both view on back button i check tagfrom condition and handle it
-(IBAction)backBtn:(id)sender
{
if ([tagfrom isEqualToString:#"present"])
{
[self dismissViewControllerAnimated:NO completion:nil];
}
else
{
[self.navigationController popViewControllerAnimated:YES];
}
}
Which working fine in both senerio, but sometimes my push view behaves like presentmodelveiw, and having no transition effects in it, please help me to resolve it
First of all, you don't need tagfrom property on your UIViewController subclass.
A UIViewController instance has a property called presentingViewController which will let you know about the viewController ( A in your case ) that presented current viewController ( C in your case ).
-(IBAction)backBtn:(id)sender
{
if (nil != self.presentingViewController) {
[self dismissViewControllerAnimated:NO completion:nil];
}
else {
[self.navigationController popViewControllerAnimated:YES];
}
}
Second, modalTransitionStyle is supposed to work with only presentation and NOT push / pop transitions. If you are using this with push / pop, the behavior is undefined because it is not meant to be used there.
I have been developing an iPhone app and have come across a few issues. I do not have a storyboard in my app and have nothing in my xibs. I have initialised and set everything up through code. When I go to the GameViewController from my main viewcontroller everything is fine, however when I come back through my back button I get this issue:
Presenting view controllers on detached view controllers is discouraged .
When I re-arrive to the main view controller, there are little changes such as the view changing before its supposed to. Here is the code for the button on my
GameViewController *game = [[GameViewController alloc] initWithNibName:nil bundle:Nil];
[self dismissViewControllerAnimated:YES completion:NULL];
[self presentViewController:game animated:YES completion:NULL];
Here is the code for the back button:
ViewController *home = [[ViewController alloc] initWithNibName:nil bundle:nil];
[self dismissViewControllerAnimated:YES completion:NULL];
[self presentViewController:home animated:NO completion:NULL];
If anyone can help me to see what I am doing wrong that would be great.
You can not present home view controller from self because it is already dismissed. You should change
[self dismissViewControllerAnimated:YES completion:NULL];
[self presentViewController:home animated:NO completion:NULL];
to
UIViewController *parentViewController = self.presentingViewController;
[self dismissViewControllerAnimated:YES completion:^
{
[parentViewController presentViewController:home animated:NO completion:nil];
}];
There are 3 view controllers View1,View2 and View3.
From view3 I have to navigate to view1.
I have tried the following code but it doesn't work.
//in View3.m
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
{
appDelegate.isComingFromCountries = YES;
[self dismissViewControllerAnimated:YES completion:nil];
}
//in View2.m
-(void)viewWillAppear:(BOOL)animated
{
if (appDelegate.isComingFromCountries == YES)
{
appDelegate.isComingFromCountries = NO;
[self dismissViewControllerAnimated:YES completion:nil];
}
}
But this code doesn't work. How do I handle this?
You can use use presentingViewController for dismissing it,
try this -
[self.presentingViewController.presentingViewController dismissModalViewControllerAnimated:YES];
A -> B -> C
Running the above code in modal C will take you back to A.
You have to use delay method to perform animation so that main thread should start performing in B viewController
[self performSelector:#selector(methodForDissmiss) withObject:nil afterDelay:0.5];
then write dismiss code in selector method to work by your logic.
If you are returning to the root view controller from a series of modally presented view controllers then the following code will work in iOS6
[self.window.rootViewController dismissViewControllerAnimated:NO completion:nil];
I think, It may help you. According to your code( in comments area), You second view controller push from first view controller, then present third from second. So you have to set delegate as second in third. And do your code as below in third VC.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
{
appDelegate.isComingFromCountries = YES;
[self dismissViewControllerAnimated:YES completion:^{
[self.delegate dismissModal];
};
}
In SecondVC.m
-(void)dismissModal
{
[self.navigationController popViewControllerAnimated:NO];
}
Also you can pop to view 1 from view 3 on like this:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
[self.navigationController dismissViewControllerAnimated:NO completion:nil];
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
You can try this one :
if you are pushing one viewController to another and want to move back then use this one
[self.navigationController popViewControllerAnimated:YES];
if you are presenting viewController use modelly and want to move back then use this one
[picker dismissViewControllerAnimated:YES completion:nil];
also you can try this because first you need to dismiss third viewController then second one.
UIViewController *viewController = [self parentViewController];
[self dismissModalViewControllerAnimated:NO];
[viewController dismissModalViewControllerAnimated:YES];
I've unsuccessfully tried:
1. [self dismissViewControllerAnimated:YES completion:Nil];
2. [[self parentViewController] dismissViewControllerAnimated:YES
completion:Nil];
3. //from the parent view controller using a delegate
-(void)closeReplaceController
{
DLog(#"closeReplaceController");
[self dismissViewControllerAnimated:YES completion:Nil];
}
I would try something like this:
-(void)killPresentingView
{
UIViewController *vc = [self presentingViewController];
[vc dismissViewControllerAnimated:YES completion:nil];
}
And see what happens. If I understood you right, this should work.