问题
What I've done so far:
I am using xcode 4.2. I created a UITableView which contains an ImageView. The UITableView uses the class MyTableViewController which has the files MyTableViewController.m/.h. I created an IBOutlet variable called _bgimageview. Then in the storyboard, in the connections inspector, i connected the ImageView to this _bgimageview variable. In my viewDidLoad function of MyTableviewController.m, I set a backgound image to the _bgimageview. When I run the simulator, click on a few tabs to get to MyTableViewController, and hten I see the background image. Things work great so far.
The problem
I've also successfully built a recursive tree, but the problem is that I lose the backgorund image after the root level of the tree. I think i know why, but don't know how to fix. I instantiate a new MyTableViewController on each row-click like so:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MyTableViewController *childView = [[MyTableViewController alloc] init];
childView._menu = children; // an NSDictionary of children
[self.navigationController pushViewController:childView animated:YES];
}
As mentioned previously, I populate the IBOutlet variable for the backgruond image in the viewDidLoad function. But that IBOutlet variable is linked up to the ImageView via the Storyboard connection. In tree levels below the root level, I programmatically instantiate new MyTableViewControllers and thus, I'm probably losing the relationship between the IBOutlet variable for the backgund image and the actual UIImageView mentioned in the Storyboard.
Can someone re-assure me this is the problem and how I might go about adding a backgorund image to MyTableViewController when used in a hierarchical manner?
回答1:
MyTableViewController *childView = [[MyTableViewController alloc] init];
This line creates a new instance of your view controller without reference to the storyboard. It will therefore not be connecting any of your outlets - the table view will probably be OK, since that is created and connected by default, but anything else will not be connected.
You can confirm this by logging your image view outlet in viewDidLoad - it will be nil.
You should create a recursive stack by dragging a segue from the row of the table down to the view controller object of the same view controller:

As Lvsti points out in comments, you can pass any parameters across in prepareForSegue:sender:
来源:https://stackoverflow.com/questions/10488556/ui-elements-and-iboutlet-variables-in-storyboard-when-instantiated-recursively