开发者

Passing data to sub view

I`m building simple app which shows information开发者_JS百科 about most car brands.

I have rootViewController to display data row (for example Mercedes, BMW, Toyota, etc.) when some of row is touched rootViewController loads view. In that view I want to display information for every car brand. All of brand description are same (year of established, models etc) Because the view structure will be the same for all car brands i will use just one view controller for displaying information.

The problem is that I can`t figure out how to do that ? How to pass description data from rootViewController to displayBrandInformation ?


You have to subclass the tableViewController that eventually pops onto the navigationcontroller stack. Inside of the subclassed tableViewController, you should have an array (or some other data structure) to hold the relevant information and it should be exposed as public (using @synthesize). Then, after you initialize the new tableView but before you push it onto the stack, set the array. For example, in your RootViewController.m:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath {
        SubmenuViewController *svc = [[SubmenuViewController alloc] initWithStyle:UITableViewStyleGrouped];
        svc.arr = someArray; //this array holds your entire Submenu, this should be based on whatever the user selected in newIndexPath.row
        [svc setTitle:@"Some Title"];
        [self pushViewController:svc animated:YES];
}

Since SubmenuViewController is a subclass of UITableViewController, inside of (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath, you will return the correct cell based on the arr array.

Edit: I should point out that in my example, RootViewController subclasses UINavigationController


Your rootViewController doesn't load a view (or shouldn't, anyway), it creates a view controller which will then manage the detail view. rootViewController should give the detail view controller whatever data it will need to do its job; in this case, that's probably an object containing the data for a particular brand.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜