Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a mainViewController with a container view in it. I'm trying to access theMainViewController from the container view.

Here is my code:
Objective-C
self.theMainViewController = (theMainViewController *)self.parentViewController;

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:idx inSection:0];
[self.theMainViewController .tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];

This doesn't work. self.theMainViewController gives a nil value when I make an nslog of it. I then replaced:

Objective-C
self.parentViewController

to:
Objective-C
self presentingViewController

and it gave me the same results. How can I access the mainViewController from the container view's class?
Posted

1 solution

It shows some weakness in your class design. You should stick to the MVC paradigma which inlcudes the separation of data, UI and handling to events.

You need to define and set a (weak) property for it. I would recommend to create a protocol and a delegate.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900