1

I am trying to access the previous viewcontroller in a navigation stack. I want to set a label's text property before I pop back to it. It crashes after the second line in the function. Thanks for any help.

func goBack(){ let i = (navigationController?.viewControllers.count)! - 1 let itemViewController = navigationController?.viewControllers[i] as ItemViewController itemViewController.typeValueLbl.text = itemName navigationController?.popViewControllerAnimated(true) } 
0

2 Answers 2

1

You are trying to set a property of your previous controller.

The ideal way of doing it would be using Protocols and Delegates.

Write a Protocol in your PUSHED class and declare a delegate property.

When your ItemViewController pushes this class, set the delegate to self.

Then you can call the delegate method, in goBack method, that sets your label text.

To learn Protocols and Delegates in Swift refer documentation

Sign up to request clarification or add additional context in comments.

1 Comment

I have done that before. I should have thought of that.
0

A quote from this thread

Here's the implementation of the accepted answer:

- (UIViewController *)backViewController { NSInteger numberOfViewControllers = self.navigationController.viewControllers.count; if (numberOfViewControllers < 2) return nil; else return [self.navigationController.viewControllers objectAtIndex:numberOfViewControllers - 2]; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.