根本不能pushViewController

时间:2012-06-19 19:18:13

标签: iphone pushviewcontroller

我通过执行以下操作将一个按钮添加到UITableView中

 // Add checkOut button
    UIView      *viewHolder =   [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 165)];
    UIButton    *checkOut   =   [UIButton buttonWithType:UIButtonTypeRoundedRect];
    CGRect      buttonRect  =   CGRectMake((self.view.frame.size.width - 220.)/2, 30, 220., 44.);

    [checkOut setTitle:@"Check out" forState:UIControlStateNormal];
    [checkOut addTarget:self action:@selector(goToCheckOut) forControlEvents:UIControlEventTouchUpInside];
    [checkOut setFrame:buttonRect];
    [viewHolder addSubview:checkOut];
    self.shoppingListTable.tableFooterView  =   viewHolder;

和goToCheckOut:

#pragma mark - goToCheckOut
- (void)goToCheckOut {
    NSLog(@"goToCheckOut");
    AnotherViewController  *controller =   [[AnotherViewController alloc] initWithNibName:@"AnotherViewController" bundle:nil];
    [self.navigationController pushViewController:controller animated:YES];
}

当我点击按钮时,日志显示

goToCheckOut

但我没有导航到另一个视图控制器。

有没有人知道为什么?请帮忙。感谢


@pgb:你对这方面有所了解。标签栏中的uiviewcontroller是我自己的视图控制器...但是,我没有uitabbarviewcontroller.Instead,我有uiviewcontroller uitabar。在点击每个uitab栏按钮时显示视图,我正在做以下

#pragma mark - TabBarDelegate
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    // firstViewController section
    if ( item.tag == 0 ) {
        [self.firstViewController.view removeFromSuperview];
        [self.secondViewControoler.view   removeFromSuperview];
        self.title   =   @"First View Controller";
        [self.view insertSubview:self.firstViewController.view belowSubview:taBar];
    }
    // secondViewController section
    else if (item.tag == 1){
        [self.firstViewController.view removeFromSuperview];
        [self.secondViewControoler.view   removeFromSuperview];
        self.title    =   @"Second View Controller";
        frame.origin.x      =   [self horizontalCoordinateAt:item.tag + 2];
        [self.view insertSubview:self.secondViewControoler.view belowSubview:taBar];
    }

}

我现在拥有的当前结构是

UIViewController0 ( it is also a navigationController)
  UIViewController1
  UIViewController2
    UITabBar
      FirstViewController
      SecondViewController

如何更改以便我可以在First和SecondViewController中执行pushViewController。

2 个答案:

答案 0 :(得分:1)

根据您的描述,我猜您在标签视图控制器中设置的UIViewController是您自己的视图控制器,而不是UINavigationController

您的层次结构必须是:

UITabViewController
   UINavigationController
     YourCustomViewController
   UINavigationController
     OtherCustomViewController

相反,我的猜测是你有以下层次结构:

UITabViewController
   YourCustomViewController
   OtherCustomViewController

self.navigationController可能不是nil,因为您可能正在创建UINavigationController,但您是 - 我想 - 无法将navigationController设置为由标签视图控制器。

答案 1 :(得分:0)

我为这个问题找到了自己的解决方案。我做的是:

1. Create a delegate with a callback method in each FirstViewController and SecondViewController
2. Make UIViewController2 conform delegate of first and second view controller
3. Implement the method in delegate ( a call back ) such as pushViewController....