1

我的问题是样本...但我仍然不知道为什么我的视图不会推送到另一个视图...

首先,我在 NSMutableArray 中添加了许多视图

static NSString *titleKey = @"title";
static NSString *viewControllerKey = @"viewController";

- (void)viewDidAppear:(BOOL)animated {
    self.menuList = [NSMutableArray array];

    FirstView *firstView = [[FirstView alloc]initWithNibName:nil bundle:nil];
    [self.menuList addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                              NSLocalizedString(@"FirstView", @""), titleKey,
                              firstView, viewControllerKey,
                              nil]];
    [FirstView release];


    SecondView *secondView = [[SecondView alloc]initWithNibName:nil bundle:nil];
    [self.menuList addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                              NSLocalizedString(@"SecondView", @""), titleKey,
                              secondView, viewControllerKey,
                              nil]];
    [SecondView release];

好的...这就是我在数组中添加视图控制器的方式...

我想当我按下表格视图中的单元格时,它会转到我要显示的视图

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UIViewController *targetViewController = [[self.menuList objectAtIndex: indexPath.row] objectForKey:viewControllerKey];
    targetViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [[self navigationController] pushViewController:targetViewController animated:YES];
}

按下单元格后,什么也没有发生

首先我猜可能是我没有正确设置数组

但是当我记录数组时

里面真的有东西...

这是结果:

Log message: MENU LIST (
        {
        title = "FirstView";
        viewController = "<Firstview: 0x5822500>";
    },
        {
        title = "SecondView";
        viewController = "<SecondView: 0x5822870>";
    }
)

我不知道为什么程序不起作用......

有没有人有任何想法???

就是这么简单的事情,还是卡在这里3个小时....

非常感谢 :)

4

3 回答 3

1

首先,您需要检查类型targetViewController以查看它是否为您提供了正确的 ViewController。如果它是正确的,那么

您确实应该检查该self.navigationController值以确保它不是nil

我怀疑第二件事是问题所在。

于 2011-01-14T07:03:22.650 回答
0

首先我不得不说,好极了,这是一个非常具有挑战性的问题。

我确定我的回答是否会有所帮助,但请听我说完。

当你使用

UIViewController *targetViewController = [[self.menuList objectAtIndex: indexPath.row] objectForKey:viewControllerKey];

您正在为 uiviewcontroller 分配 firstview 或 secondview 类型的类,这就是我的问题。

但这是正确的,因为父类可以代替子类(假设 2 类已经继承了 uiviewcontroller)。

所以你可以在

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

在控件到达函数末尾之前,将光标放在 targetViewController 上并检查其内容。

如果它有 firstview 或 secondview ,它将显示变量的类型 firstview 和 secondview 是。

于 2011-01-14T06:38:58.370 回答
0

为了成功进行推送操作,您需要在应用程序委托中创建一个 UINavigationController 并使用根视图控制器(根视图控制器 = FirstView-> 我希望这是一个 UIViewController)。

完成此操作后,您可以在 FirstView 中调用

[[self navigationController] pushViewController:targetViewController animated:YES];

为了在堆栈上推送另一个 UIViewController。

UINavigationController 类实现了一个专门的视图控制器来管理分层内容的导航。此类不用于子类化。相反,在您希望应用程序的用户界面反映内容的分层性质的情况下,您可以按原样使用它的实例。此导航界面可以有效地呈现您的数据,也使用户更容易导航该内容。

这是 UINavigationController 文档的官方链接,它们很有用。 链接到 UINavigationController 文档

于 2011-01-14T07:24:57.543 回答