Calling a function when a view controller is dismissed (xcode/iOS) - xcode

I have a main view controller. When a specific button is pressed, I present another view controller. When the second view controller is dismissed, I want to call a function in the main view controller. Originally I thought I could just use the completion handler for presentviewcontroller, but now I'm thinking that is not the way to go. Any tips on how I can accomplish this? I'm pretty new to this, so any help would be appreciated!

Assuming your main view controller is the second view controller's parent, you can access the method with:
[[secondViewController parentViewController] method];
Beware -- if you are using a navigation controller, the parent view controller will be the navigation controller. In this case, you will have to query the navigation controller's stack.
Good luck!

Related

What happened to "Is initial view controller"checkbox?

I just added a second view controller to my project, but when I went to set is as the initial view controller, the very convenient checkbox was missing (the Title label is also missing). I embedded it into a navigation controller just to see what was up, and the checkbox was available for the nav controller. We used to be able to have a project with two view controllers, no nav controller, and be able to simply check the box to set which was the initial. Is this a bug or a deliberate move away from the checkbox in anything that's not a navigation controller?
View controller (no checkbox):
Navigation controller:
That Inspector's contents look a lot like you have selected the view controller's main view, not the view controller itself.

Swift back button with segue show not showing

So I tried everything with showing up the back button but it just won't work
This is how i arrange my View Controllers Used segue show from view controller to site view controller
And here is the site view controller that isn't showing back button
I even tried adding NavigationBar and a button with this function
self.navigationController.popToRootViewControllerAnimated(true)
Also failed
Update:
I tried this in both view controllers it's returning nil
in viewDidAppear and viewWillAppear
print(self.navigationController?.restorationIdentifier)
Once I got the same problem, when I was try to use view property of my UIViewController before it loaded properly. This caused problems with loading my navigation bar items. In fact, you should not touch view property until viewDidLoad method is called. Check, maybe it is your case, maybe you use view property in prepareForSegue method or in observers?

Create a segue to a different NavigationController

I have an app that has two navigation controllers. I'm trying to create the segue shown in the red arrow below: Pressing the "+" button should take you to a specific view controller of another NavigationController.
I've been able to successfully go to this view controller but it doesn't have any navigation properties (Pressing "Back" doesn't do anything).
How do I jump to a view controller in a different navigation controller but still retain navigation properties?
I will say you want to move from controller A to controller B for reference.
Each UINavigationController instance has a viewControllers array that stores the controllers in the navigation stack.
In your case, the first navigation controller has 2 view controllers in the navigation stack, the same thing for the second navigation controller.
If you press back in the view controller B, it should pop to one of the view controllers in the UINavigationController stack to which B belongs. Since controller A et B don't belong to the same UINavigationController, hence you cannot access the pop API to move back.
One way is to present controller B modally from controller A instead of pushing it.

How can I update a view controller when something happens in a container view controller?

What I need to do is be able to update my main view controller when something happens in a container view inside of it.
For example, if there was a nav bar at the top of a page with a save button. Below the nav bar is a container view and a form. The save button is greyed out until text is entered into the form and then it becomes intractable with the user. I want to do something similar so that the main view controller responds to something happening on the container view.
I can use a singleton create a boolean variable that changes when I want my code to run, but how can I make it so that once this variable changes to true, code on the other view controller runs and responds to this change?
I saw in this post that I could use delegates but it still leaves me with the problem of how the other view controller detects that the variable has changed - the method in this post was just another way of making the variable accessible from the other view controller I think.
I thought that I could have a while loop running checking the variable but this would freeze the main view controller until the while loop had finished - in this case when the variable was true wouldn't it?
Is there a way for a view controller to check if a variable has changed so that once it has, certain code can run afterwards without if freezing the view controller like I think a while loop would? Or a way for a view controller to respond to the value of a variable?
Thank you.
Have the main view controller listen for a NSNotification that tells it something needs saving. When text is entered, the controller that looks after the text field sends the notification.
You can attach the new text (or other information) to the notification if the target needs to know about its details.

iOS8 UISplitViewController: How to switch to master view in compact width window?

My root view controller is an UISplitViewController, which has a UITableViewController as master view controller. On iPhone (compact width), it looks like a UINavigationController.
Tap a cell to show detail view controller
Tapping the trash button would delete the current note. My problem is how to go back to the master view after that? Since it's an UISplitViewController, it can't pop the current view controller as UINavigationController does.
I had a similar problem and finally found a solution. As I understand it, when in compact width, the detail navigation controller becomes a view controller of the master navigation controller. So all you have to do is:
Determine if only one view is present by checking the split view controller's collapsed property. If it isn't collapsed (e.g. on iPad), you're already showing the table view in addition to the detail view.
If it is collapsed (e.g. on iPhone) get a reference to the master navigation controller via the detail navigation controller and have it pop to its root view controller which, in this case the your table view controller.
This is the code I use in my detail view controller. In your case I think you just need to add this code to the button action in your detail view controller:
if splitViewController!.collapsed {
let detailNavController = parentViewController as UINavigationController!
let masterNavController = detailNavController.parentViewController as UINavigationController!
masterNavController.popToRootViewControllerAnimated(true)
}
Good luck!

Resources