viewWillAppear of parent triggered when second presented viewcontroller dismissed - ios

I have 3 viewcontrollers
let's say AVC, BVC and CVC
First I present BVC from AVC and then present CVC from BVC
So the hierarchy would be AVC->BVC->CVC.
When I dismiss CVC from BVC, viewWillAppear of AVC is triggered, how can I prevent viewWillAppear of AVC getting called?
Thanks
UPDATE
First present (in AVC)
BTViewController *BTVC = [self.storyboard instantiateViewControllerWithIdentifier:#"BTVCIns"];
__weak BTViewController *weakBTVC = BTVC;
BTVC.dismissAction = ^{[weakBuyTicketVC dismissViewControllerAnimated:YES completion:nil];};
[BTVC setModalPresentationStyle:UIModalPresentationOverCurrentContext];
[self presentViewController:BTVC animated:YES completion:nil];
Second present (in BVC)
PMViewController *PMVC = [self.storyboard instantiateViewControllerWithIdentifier:#"PMVCIns"];
__block PMViewController *blockPMVC = PMVC;
blockPMVC.dismissAction = ^{[blockPMVC dismissViewControllerAnimated:YES completion:nil];};
blockPMVC.delegate = self;
[self presentViewController:PMVC animated:YES completion:nil];

From the View Controller Programming Guide for iOS
When presenting a view controller using the UIModalPresentationFullScreen style, UIKit normally removes the views of the underlying view controller after the transition animations finish. You can prevent the removal of those views by specifying the UIModalPresentationOverCurrentContext style instead. You might use that style when the presented view controller has transparent areas that let underlying content show through.
Since you are using UIModalPresentationOverCurrentContext, iOS considers that BVC may have transparent areas which expose AVC, hence when BVC appears it is possible that AVC is also going to appear, so viewWillAppear is called.

Related

ViewController WILL NOT dismiss

WLINewPostViewController *newPostViewController = [[WLINewPostViewController alloc] initWithNibName:#"WLINewPostViewController" bundle:nil];
UINavigationController *newPostNavigationController = [[UINavigationController alloc] initWithRootViewController:newPostViewController];
newPostNavigationController.navigationBar.translucent = NO;
[tabBarController presentViewController:newPostNavigationController animated:YES completion:nil];
So I just simply push a new UIViewController.
Then after it posts the server callback calls a method with this code from the WLINewPostViewController.m:
[self dismissViewControllerAnimated:YES completion:^{
NSLog(#"Completed");
}];
[[self navigationController] popViewControllerAnimated:YES];
if (self == self.navigationController.visibleViewController){
NSLog(#"self = visibile");
}
if (self == self.presentingViewController.presentingViewController){
NSLog(#"self = presenting");
}
}
I tried a bunch of different things and none work.
I am relatively new to Xcode but after trying
[self dismissViewControllerAnimated:YES completion]
[self.navigationController popViewControllerAnimated:YES]
[self.navigationController.visibleViewController.presentedViewController dismissViewControllerAnimated:YES completion:nil];
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
and every other possibility, I am officially stumped. The WLINewPostViewController still won't dismiss.
It Logs out "self = visible"
Let me illustrate what you are trying to do
You have a navigation controller with Controller A.
Here you are trying to present another Controller B from Controller A.
Now when you get a callback from the server, you should call dismissViewControllerAnimated from Controller B to dismiss itself.
So after dismissViewControllerAnimated:completion: method call, the Controller B will be dismissed and Controller A will be shown automatically. Now you do not need to call popViewControllerAnimated: in completion block again as there is no other Controller in navigation controller to load.
If you have different use case, let me know I can provide solution.
You are presenting a view over navigationbar instead of pushing it over navigationbar.
When push you pop. When you present you dismiss. So instead of popViewControllerAnimated you need to use dismissViewControllerAnimated:completion
dismiss behaves differently depending on the receiver. From the docs:
The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, it automatically forwards the message to the presenting view controller.
If you present several view controllers in succession, thus building a stack of presented view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. When this happens, only the top-most view is dismissed in an animated fashion; any intermediate view controllers are simply removed from the stack. The top-most view is dismissed using its modal transition style, which may differ from the styles used by other view controllers lower in the stack.
In short, if the vc on top calls it on itself, it dismisses itself. Anywhere else on the stack dismisses to that point, animating only the topmost vc.
What's extra confusing (for you and many others) is that the navigation vc has a stack too, and your problem is complicated further by presenting an navigation vc atop a tab-bar vc.
So what to do? The question is unclear about which vc is the receiver in the posted code (who is self in that snippet?). The text implies that self is a vc on the stack of the presented navigation vc, like...
TabBarVC --- presents ---> NavVC
| |
| --- viewControllers stack = rootVC, vc1
|
---> viewControllers for each tab
... and it's root or vc1 that wants to dismiss. If I'm right about that, then, given the docs, the solution is clear:
[self.navigationController dismissViewControllerAnimated:YES completion:^{}];
will put us back on the tabbar vc on whatever tab was visible when we did the present.

Dismissing a ViewController lower in the stack does not behave as expected

I'm building a complex app that has kind of a branch in the middle.
At some point in the app, a particular UIViewController is presented, we'll call it mainViewController (shortened mainVC).
The mainVC presents another view controller, by code, using the following code (I strip out parts of it for privacy reasons):
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"SecondaryStoryboard" bundle:secondaryBundle];
SecondViewController *secondVC = [storyboard instantiateInitialViewController];
[self presentViewController:secondVC animated:YES completion:nil];
So the secondVC will later present another view controller, called thirdVC. This is done using a custom segue, set in the storyboard used in the code above, which code looks like this:
#implementation VCCustomPushSegue
- (void)perform {
UIView *sourceView = ((UIViewController *)self.sourceViewController).view;
UIView *destinationView = ((UIViewController *)self.destinationViewController).view;
UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
destinationView.center = CGPointMake(sourceView.center.x + sourceView.frame.size.width, destinationView.center.y);
[window insertSubview:destinationView aboveSubview:sourceView];
[UIView animateWithDuration:0.4
animations:^{
destinationView.center = CGPointMake(sourceView.center.x, destinationView.center.y);
sourceView.center = CGPointMake(0 - sourceView.center.x, destinationView.center.y);
}
completion:^(BOOL finished){
[self.sourceViewController presentViewController:self.destinationViewController animated:NO completion:nil];
}];
}
#end
As you can see this segue presents the destination view controller modally (by the use of presentViewController:) with a custom animation (a slide from right to left).
So basically up to here everything is fine. I present the secondVC with a classic modal animation (slide up from bottom) and present the thirdVC with my custom transition.
But when I want to dismiss the thirdVC, what I want is to go back directly to the mainVC. So I call the following from the thirdVC :
self.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self.presentingViewController.presentingViewController dismissViewControllerAnimated:_animate completion:nil];
That way, I'm calling dismissViewControllerAnimated: directly on mainVC (referenced by self.presentingViewController.presentingViewController), and I'm expecting the thirdVC to be dismissed with an animation, and the secondVC to just disappear without animation.
As Apple says in the UIViewController Class Documentation:
The presenting view controller is responsible for dismissing the view
controller it presented. If you call this method on the presented view
controller itself, it automatically forwards the message to the
presenting view controller.
If you present several view controllers in succession, thus building a
stack of presented view controllers, calling this method on a view
controller lower in the stack dismisses its immediate child view
controller and all view controllers above that child on the stack.
When this happens, only the top-most view is dismissed in an animated
fashion; any intermediate view controllers are simply removed from the
stack. The top-most view is dismissed using its modal transition
style, which may differ from the styles used by other view controllers
lower in the stack.
The issue is that it's not what happens. In my scenario, the thirdVC disappears, and shows the secondVC being dismissed with the classic modal slide to bottom animation.
What am I doing wrong ?
Edit :
So #codeFi's answer is probably working in a classic project, but the problem here is that I'm working on a framework. So mainVC would be in a client app, and the secondVC and thirdVC are in my framework, in a separate storyboard. I don't have access to mainVC in any other way than a reference to it in my code, so unwind segues are unfortunately not an option here.
I've been having this exact same issue, and I've managed to visually work around it by adding a snapshot of the screen as a subview to secondVC.view, like so:
if (self.presentedViewController.presentedViewController) {
[self.presentedViewController.view addSubview:[[UIScreen mainScreen] snapshotViewAfterScreenUpdates:NO]];
}
[self dismissViewControllerAnimated:YES completion:nil];
Not pretty, but it seems to be working.
NOTE: if your secondVC has a navigation bar, you will need to hide the navigation bar in between snapshotting the screen and adding the snapshot as a subview to secondVC, as otherwise the snapshot will appear below the navigation bar, thus seemingly displaying a double navigation bar during the dismissal animation. Code:
if (self.presentedViewController.presentedViewController) {
UIView *snapshot = [[UIScreen mainScreen] snapshotViewAfterScreenUpdates:NO];
[self.presentedViewController.navigationController setNavigationBarHidden:YES animated:NO];
[self.presentedViewController.view addSubview:snapshot];
}
[self dismissViewControllerAnimated:YES completion:nil];
I had the same issue and I've fixed it by using UnwindSegues.
Basically, all you have to do is add an IBAction Unwind Segue method in the ViewController that you want to segue to and then connect in IB the Exit action to your Unwind Segue method.
Example:
Let's say you have three ViewControllers (VC1, VC2, VC3) and you want to go from VC3 to VC1.
Step 1
Add a method to VC1 like the following:
- (IBAction)unwindToVC1:(UIStoryboardSegue*)sender
{
}
Step 2
Go in Interface Builder to VC3 and select it. Then CTRL-drag from your VC icon to Exit icon and select the method you've just added in VC1.
Step 3
While still in IB and with VC3 selected, select your Unwind Segue and in the Attributes Inspector add a Segue Identifier.
Step 4
Go to VC3 where you need to perform your segue (or dismiss the VC) and add the following:
[self performSegueWithIdentifier:#"VC1Segue" sender:self];

IOS 7 : Adding modal ViewController with transparency : Custom Size

What I want to do is present a viewController that comes from the bottom and has a custom size. Whatever I seem to do either looks naff (e.g its animated up, flashes then goes transparent), is fullscreen or blacks out the whole screen. I have read a few solutions with custom delegates handing the animation but there must be a simple way. Also a lot of the solutions are for iOS 6 <
HackViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"HackViewController"];
vc.delegate = self;
vc.modalPresentationStyle= UIModalPresentationCustom;
vc.view.frame = CGRectMake(0, 0, 800, 400);
vc.credits = CREDIT_PER_LEVEL * building.buildingLevels;
vc.view.backgroundColor = [UIColor clearColor];
[self presentViewController:vc animated:YES completion:^{
vc.view.backgroundColor = [UIColor clearColor];
}];
If you wanna add viewController (say VC2) as a modal over first viewController(say VC1), then in VC1, when you are presenting VC2,
[self presentViewController:VC2 animated:YES completion:nil];
In VC2 viewDidLoad
[self.view setBackgroundColor:[UIColor colorWithRed:211.0/255.0 green:211.0/255.0 blue:211.0/255.0 alpha:0.8]];
And, in AppDelegate under application didFinishLaunchingWithOptions:
[[self.window rootViewController] setModalPresentationStyle:UIModalPresentationCurrentContext];
You can create other views like imageView or label, etc in VC2
If you only want to support iOS7 and above, you can implement this using a custom modal presentation style (UIModalPresentationCustom) and implement the transitioningDelegate to return a transitioning controller. In the transitioning controller, you will be called when the presentation is to happen, and you can provide the target frame of the presented view controller. This has a big advantage of using the system presentation model, and the system is aware a controller has been presented.
If you need to support iOS6, you need to add the presented view controller's as a subview, the view controller as a child view controller of the presenting view controller, and manager the view hierarchy yourself. This is less than optimal because of manual view hierarchy control and child/parent view controllers.
You don't have to presentViewController:, you can do that with addSubview:
Keep doing what you are doing but add view of vc to view of presented view controller instead of presenting vc with presentViewController:
If you do that within an animation block, it would look nice.

Dismissing view controller to reveal desired one on the backside

Suppose i have 3 view controllers. I initiate vc1 in vc0 and then vc2 in vc1. Now i want vc2 to be dismissed and after that only vc0 should be shown behind it. How's that possible? I read something about delegate declaration. But couldn't understand.
I have UISegmentcontrol where i'm displaying a controller from storyboard like - vc0 = [self.storyboard instantiateViewControllerWithIdentifier=#"vc0"]; and making it a subview of it [self.view addSubview:vc0.view]; Vc0 is a tableView, which has a detailcontroller to be presented. When i tap on a cell, it shows detailview, but actual segmentcontrol.view is lost when detailview is dismissed.
An example would be awesome.
PS: I'm not using segue for the viewControllers. Instead, i'm using presentModalViewController and dismissModalViewController.
In iOS 5 you need to do
[self.presentingViewController.presentingViewController dismissModalViewControllerAnimated:YES]
As of iOS 6 dismissModalViewControllerAnimated: is deprecated.
You need to call
[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:^{ // Do something on completion}]
//If you want to perform something while going through views or other wise keep completion to nil as shown in below code.
or
[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
or
[self.parentViewController.parentViewController dismissModalViewControllerAnimated:YES];

I am really confused regarding few things in UIViewController

I am really confused regarding few things in UIViewController, I have already read the View Controller Programming Guide and searched lot on the Internet but still confused.
When I want to jump or switch from firstVC to secondVC how many types of methods are available? I am listing which I know:
UINavigationController
UITabBarController
presentModalViewController:
Add secondVC to root view
If secondVC is added to root view then how firstVC object will be released?
Is it a good practice to add every view I want to jump/switch to root view?
transitionFromView:
I dont understand this portion from Apple doc:
This method modifies the views in their view hierarchy only. It does
not modify your application’s view controllers in any way. For
example, if you use this method to change the root view displayed by a
view controller, it is your responsibility to update the view
controller appropriately to handle the change.
If I do this:
secondViewController *sVc = [[secondViewController alloc]init];
[transitionFromView:self.view toView:sVc.view...
Still viewDidLoad:, viewWillAppear:, viewDidAppear: are working fine: I don't need to call them. So why did Apple say this:
it is your responsibility to update the view controller appropriately to handle the change.
Are there any other methods available?
Actually the standard methods used are :
1) Using NavigationController
//push the another VC to the stack
[self.navigationController pushViewController:anotherVC animated:YES];
//remove it from the stack
[self.navigationController popViewControllerAnimated:NO];
//or presenting another VC from current navigationController
[self.navigationController presentViewController:anotherVC animated:YES completion:nil];
//dismiss it
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
2) Presenting the VC
//presenting another VC from current VC
[self presentViewController:anotherVC animated:YES completion:nil
//dismiss it
[self dismissViewControllerAnimated:YES completion:nil];
Never use the method you described in points 4. It's not a good practice to change root view controller's dynamically. window's root VC is usually defined on applicationdidfinishlaunchingwithoptions after that it shouldn't be changed , if you are to follow apple standards.
Example for transitionFromView:toView
-(IBAction) anAction:(id) sender {
// assume view1 and view2 are some subviews of self.view
// view1 will be replaced with view2 in the view hierarchy
[UIView transitionFromView:view1
toView:view2
duration:0.5
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:^(BOOL finished){
/* do something on animation completion */
}];
}
}

Resources