【问题标题】:How to pushviewcontroller to a viewcontroller stored in a tabbaritem?如何将视图控制器推送到存储在 tabbaritem 中的视图控制器?
【发布时间】:2010-04-09 18:32:03
【问题描述】:

首先我知道这是一个很长的问题。放心我已经尝试自己解决了(请参阅:StackOverflow #2609318)。这让我很兴奋!

在尝试在标准 moreNavigationController 中实现我自己的 EDIT 功能但失败后,我决定重新实现我自己的 MORE 功能。

我做了以下事情:

添加一个我初始化的 HOME 视图控制器:initWithRootViewController

添加 3 个其他默认选项卡:

ResortsListViewController *resortsListViewController;
resortsListViewController = [[ResortsListViewController alloc] initWithNibName:@"ResortsListView" bundle:nil];
resortsListViewController.title = [categoriesDictionary objectForKey:@"category_name"];
resortsListViewController.tabBarItem.image = [UIImage imageNamed:@"whatever.png"];
resortsListViewController.navigationItem.title=@"whatever title";
localNavigationController = [[UINavigationController alloc] initWithRootViewController:resortsListViewController];
localNavigationController.navigationBar.barStyle = UIBarStyleBlack;
[localControllersArray addObject:localNavigationController]; 
[localNavigationController release];
[resortsListViewController release];

当我将它们添加到标签栏时,它们会起作用。 (即:点击它们,它会转到视图控制器)

然后我将自己的 MORE 视图控制器添加到标签栏:

MoreViewController *moreViewController;
moreViewController = [[MoreViewController alloc] initWithNibName:@"MoreView" bundle:nil];
moreViewController.title = @"More";
moreViewController.tabBarItem.image = [UIImage imageNamed:@"more.png"];
moreViewController.navigationItem.title=@"More Categories";
localNavigationController = [[UINavigationController alloc] initWithRootViewController:moreViewController];
localNavigationController.navigationBar.barStyle = UIBarStyleBlack;
[localControllersArray addObject:localNavigationController]; 
[localNavigationController release];
[moreViewController release];

然后

tabBarController.viewControllers = localControllersArray;
tabBarController.moreNavigationController.navigationBar.barStyle = UIBarStyleBlack;  
tabBarController.customizableViewControllers = [NSArray arrayWithObjects:nil];
tabBarController.delegate = self;

这创建了必要的联系。好的,到目前为止一切都很好。我得到一个 HOME 选项卡、3 个类别选项卡和一个自定义的 MORE 选项卡——它们都可以工作。

在 MORE 选项卡视图控制器中,我实现了一个简单的表格视图,它显示了我在行中拥有的所有其他选项卡。因为我希望能够将它们切换进和切换出标签栏,所以我创建了它们,就像我在上面的 Resortslistviewcontroller 中所做的一样(即:作为数组中的视图控制器)。当我将它们拉出以在表格视图中显示标题时(以便用户可以转到该“视图”),我只需执行以下操作:

    // [myGizmoClass CategoryArray] holds the array of view controller tab bar items that are NOT shown on the main screen.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ... etc...

        UIViewController *Uivc = [[myGizmoClass plusCategoryArray] objectAtIndex:indexPath.row];
        cell.textLabel.text = [Uivc title];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

这就是它的落脚点:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    MyGizmoClass *myGizmoClass= [MyGizmoClass sharedManager];

    UIViewController *tbi = [[myGizmoClass plusCategoryArray] objectAtIndex:indexPath.row];
    NSLog(@"%@\n",[[tbi navigationItem ]title]);

    [self.navigationController pushViewController:tbi animated:YES];

}

这是我得到的错误(“ATMs”是单击的表格视图单元格的标题)所以我知道 Uivc 标题正在提取正确的标题,因此是正确的“objectatindex”:

2010-04-09 11:25:48.222 MouseAddict[47485:207] 自动取款机 2010-04-09 11:25:48.222 MouseAddict[47485:207] *** 由于未捕获的异常而终止应用程序 'NSInvalidArgumentException',原因: '推导航控制器是 不支持'

大问题:我如何使UIViewController *tbi 的相关视图显示并被推送到视图中?

我猜UIViewController 是这个tbl 的正确类。我不确定。但我只想获得视图,以便将其推入堆栈。

有人可以帮忙吗?

在下面回答 kovpas 的问题:myGizmoClass 是一个单例(苹果的单例 myGizmo 类。视图控制器数组存储在其中,就像它在 [localControllersArray addObject:localNavigationController]; 中一样(在第一个代码 sn -p 上面)。并且它确实将其放入并正确拔出,这一事实证明,当我 NSLOG 时[Uivc title] 日志打印ATMs。这意味着 plusCategoryArray 正确存储和检索 viewController(如果,确实,这就是存储的内容)。

Pushing a navigation controller is not supported 真的很困扰我。为什么viewController 会返回navigationController 并且是否可以强制navigationController 从中获取“可推送”视图......或者navigationController 中有一些元素是视图?

【问题讨论】:

    标签: iphone uiviewcontroller uinavigationcontroller uitabbarcontroller uitableview


    【解决方案1】:

    从错误来看,您的 Gizmo 类似乎有一个 UINavigationControllers 数组,而不是 UIViewControllers。所以改为推送:

    [self.navigationController pushViewController:[[tbi viewControllers] lastObject] animated:YES];
    

    如果该数组与您在上面调用的 localControllers 相同,那么这应该会更好。或者您可以只创建没有 UINavigationControllers 的数组,如果您要将它们推送到更多控制器导航控制器上,则不需要它们。

    【讨论】:

    • 我需要它们,因为我希望能够编辑此列表并将相同的控制器放在标签栏项目上,如果我需要的话。将尝试您的选择。
    • 你是圣人!有效!!!!检查你的答案!非常感谢您如此迅速地回答并提供所需的确切代码!
    • [[tbi viewControllers] lastObject]可以写成[tbi topViewController]
    【解决方案2】:

    我不确定,但是当您尝试将 UINavigationController 推送到另一个 UINavigationController 时,似乎会出现此错误。您能否提供一个 MyGizmoClass 的实现?

    【讨论】:

      猜你喜欢
      • 2010-12-11
      • 1970-01-01
      • 2016-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多