Background UIViewController loading when ModalViewController is in the foreground (iOS) - objective-c

I'm not sure why this is happening, but in the App delegate, I'm setting a viewcontroller as the root and presenting a ModalViewController in front of it. The strange thing is that the viewcontroller behind the modalviewcontroller is still loading even though it does not appear. Is this normal? Is there a way to prevent the viewcontroller behind the modalviewcontroller to load?
Thank you

I am not sure exactly what you mean by "loading." As long as the view controller in the background is the parent view controller of the modal view controller, it is going to need be initialized before the modal view controller can be displayed at all. In that sense, it must be loaded.
If, on the other hand, you mean that the contents of the background view controller are visually displayed and then, afterwards, the model view controller appears on top of it—but you don't want the contents of the background controller to be seen until after the modal view controller is dismissed—then one possibility would be to set the hidden property to YES and unhide the contents when modal view controller is dismissed. To do that, I would make the background view controller a delegate of the modal view controller, so it receives a callback when the modal view controller is dismissed.

Related

How to modify objects within a modal view?

I have an app with several views. Taking into consideration the large main view, called MyView1, it is controlled by MyView1Controller. Within MyView1, there is a button that causes a modal segue to another view, whose controller is also MyView1Controller. This modal view has a couple UILabels, and a button that terminate the modal view, bringing the user back to MainView1.
Here is the problem... Let's say in my modal view there is a UILabel called sampleLabel. While in MyView1, a button is pressed, which executes the code:
sampleLabel.text = #"changed";
Since the UILabel named sampleLabel is not on screen for MyView1, and instead is part of the modal view from MyView1, nothing happens. However, when I click on the button to view the modal view from MyView1, the UILabel hasn't changed.
This is even more puzzling since the main MyView1 and the modal view that segues off of MyView1 are controlled by the same view controller, MyView1Controller.
Can someone please tell me how I can make code that executes during the user's interaction with MyView1 change things in the modal view, so that when they press the button and segue to the modal view, the UILabel's have already been changed?
Thanks!
First of all, Apple recommends (and it makes life a lot easier) to have one view controller for each view. So you should have a second view controller. In the second view controller you would have a property called sampleLabel. In the first view controller you could use different methods to set the sampleLabel.text. I would probably create a separate sampleLabelText property in the first view controller (could be an NSString *) and set it to the text you want when the user presses a button. Then in your
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
you would get your second view controller and set its property like this:
SecondViewController *svc = [segue destinationViewController];
svc.sampleLabel.text = self.sampleLabelText;
That's it. Hope this helps.
So I have had a similar issue that I resolved through 'delegation' but not through a segue schema. Here is a link to the stackoverflow question and my answer. Delegation
Hopefully this gets you going in the right path. Instead of modally presenting a view, I push a new viewcontroller onto a navigation stack but the same answer should apply, hopefully :P

How to get notification in view controller when bringSubviewToFront is called?

My application needs a notification in the view controller when I call bringSubviewToFront(). I tried viewDidAppear but it doesn't work (Obviously I shouldn't work because view is already in the stack). I have gone through the documentation of view & viewController but didn't find anything that can solve my problem. Is there any way to get view controller notified?
You won't get the viewDidAppear notification with bringSubviewToFront. It gets fired when you add the view as a subview or change the hidden property on the view. You must also make sure that your ViewController has been added as a child view controller to it's parent using
addChildViewController.

Performing action in root view controller after dismissing modal view controller

If I wanted to perform some sort of action right after dismissing a modal view controller, where exactly would I put the code? I know it wouldn't be in viewDidLoad because the rootviewcontroller was already loaded into memory (because it was just temporarily hidden by the modal view) and I'm not sure about viewWillAppear or viewDidAppear because of course the view has already been loaded into memory.
ViewWillAppear will be called again in caller view controller when modal is dismissed, so with some logic can be a good place.
Otherwise you can think about implementing a delegate, or if synchronicity is not an option to be considered, you can send a custom NSNotification to be catched by the modal caller.

Custom UIViewController transition with rotation

I have a couple of UIViewController subclasses. One that displays a grid and another that is essentially the 'detail view' for the tile on the grid. The user taps a tile and the detail view expands from the grid to fill the screen.
I have managed to get the views to animate properly and can display the content of each view perfectly. My problem comes when the device is rotated. I have displayed the detail view as a subview of the gridview and so when I rotate the device, the grid view controller gets the rotation calls, not the detail view.
As this is a custom animation, I couldn't use the standard pop and push view controller methods. Is there an method that I have to call to make this view controller responsible for handling rotation until it is dismissed?
Thanks
If I understand correctly, you rotate your grid view controller and it responds, but the 'detail view' view controller doesn't change correctly?
If this is the case, there are two possible solutions I can think of (and currently use myself). One solution would be to register the 'detail view' for a notification. Whenever the grid view controller is rotated, send the notification and the 'detail view' should respond as you want it.
The other solution is just a simple check when the 'detail view' is loaded.
if (self.view.bounds.size.height < self.view.bounds.size.width) {
// apply the code you wish to size it for landscape mode;
}
This, of course, would only work if the 'detail view' is not currently visible when the screen is rotated, so sending a notification may be a better choice to cover all possibilities.
You could set up a custom delegate in the grid view controller and register the detail view controller as that delegate after it is set up. In the grid view controller you then implement willRotateToInterfaceOrientation:duration: and didRotateFromInterfaceOrientation: which first call the corresponding methods in the delegate (the detail view controller) and then in super.

Changing a view after modalViewController is dismissed (Cocoa Touch)

I have a viewController that presents a modalViewController when a UINavigationBarItem is touched. After the user is done with the modal view, the modalViewController is dismissed. At this point I want to change/remove the NavigationBarItem -- how would I accomplish this?
I was thinking of using viewWillAppear: but I only want the change to happen when the modalViewController is dismissed.
One way is to use NSNotificationCenter.
Before presenting the modal view, call addObserver to prepare to be notified.
At the place where the modal view is dismissed, post a notification using postNotificationName.
The notification will call a method you specify in addObserver.
In this method, put your "modal view dismissed" logic.