Reputation: 277
I have a custom segue type (overriding init and perform methods of UIStoryboardSegue) and in init method I instantiate the destination view controller(VC). In prepareForSegue method of source VC I call a method of the destination VC that tries to reload the tableView of the destination VC. The problem is that the table view is not always initialized and I SOMETIMES get a nil de-reference error when I call the reloaddata of the tableview. The question is that how can I wait till the VC is fully initialized and do not get this error? I am using swift and would appreciate if you write any sample code for the answer in swift.
Upvotes: 2
Views: 2873
Reputation: 4728
just make a call on the viewController's view to force its load.
[viewController view]; //will force a loadView if necessary
///then do what you're trying to do..
Upvotes: 5
Reputation: 72780
I think that the best approach in this case is to add a flag property in the destination VC, something like:
var forceReload: Bool
that you set from prepareForSegue
in the source VC. This way, you can choose where to actually perform the data reload from the destination VC (for example, in viewDidLoad
or viewWillAppear
) by simply checking the value of that flag - of course if the flag is true, don't remember to reset it.
If you also need to pass data from the source to the destination VC, use one or more properties declared in the destination and set from the source.
Upvotes: 0