Swift - using popViewController and passing data to ViewController you return to

I have an optional bool variable called showSettings on my first view controller called ViewController and I am returning from SecondViewController back to ViewController .

Before I pop, I want to set bool to true. It seems wrong to instantiate another view controller since the ViewController is in memory.

What is the best way to do this? I do not use storyboards if this is important for your answer.

thanks for the help

+6
source share
3 answers

So, I understood, based mainly on this post - http://makeapppie.com/2014/09/15/swift-swift-programmatic-navigation-view-controllers-in-swift/

In the SecondViewController , above the class declaration, add this code:

 protocol SecondVCDelegate { func didFinishSecondVC(controller: SecondViewController) } 

Then inside the SecondViewContoller add a class variable:

var delegate: MeditationVCDelegate! = nil

Then, inside your function that your button is aiming at, add the following:

 self.navigationController?.popViewControllerAnimated(true) delegate.didFinishSecondVC(self) 

What we do here does pop in the SecondViewController and does not pass any data, but since we defined the protocol, we will use this in the ViewController to process the data.

So, in the ViewController add the protocol that you defined in SecondViewController to the SecondViewController 's list of classes inherited from:

 class ViewController: UIViewController, SecondVCDelegate { ... your code... } 

You need to add the function defined in the new protocol to make the compiler happy. Inside the ViewController class ViewController add the following:

 func didFinishSecondVC(controller: SecondViewController) { self.myBoolVar = true controller.navigationController?.popViewControllerAnimated(true) } 

In SecondViewController , where we call didFinishSecondVC , we call this method inside the ViewController class, to the controller that we are accessing. It is like we wrote this code inside the SecondViewController , but we wrote it inside the ViewController , and we use the delegate to control the messaging between them.

Finally, in the ViewController in the function that we aim to click on SecondViewController , add this code:

 let secondVC = secondViewController() secondVC.delegate = self self.navigationController?.pushViewController(secondVC, animated: true) 

What is it! You must be set up to pass code between two view controllers without using a storyboard!

+15
source
 _ = self.navigationController?.popViewController(animated: true) let previousViewController = self.navigationController?.viewControllers.last as! PreviousViewController previousViewController.PropertyOrMethod 
+8
source

I stumbled upon this, looking for a way to do this. Since I use Storyboards more often, I found that I can get an array of controllers in the navigation stack, get the one in front of the current one, check if it is my delegate, and if so, drop it as a delegate, set my methods, and then pop out of the stack. Although the code is in ObjC, it should be easily translated to fast:

 // we need to get the previous view controller NSArray *array = self.navigationController.viewControllers; if ( array.count > 1) { UIViewController *controller = [array objectAtIndex:(array.count - 2)]; if ( [controller conformsToProtocol:@protocol(GenreSelectionDelegate)]) { id<GenreSelectionDelegate> genreDelegate = (id<GenreSelectionDelegate>)controller; [genreDelegate setGenre:_selectedGenre]; } [self.navigationController popViewControllerAnimated:YES]; } 
0
source

Source: https://habr.com/ru/post/989500/


All Articles