2

我通过执行以下操作将按钮添加到 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。相反,我有uiviewcontroller其中uitabar。要在单击每个 uitab bar 按钮时显示视图,我正在执行以下操作

#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

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

4

2 回答 2

1

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

您的层次结构需要是:

UITabViewController
   UINavigationController
     YourCustomViewController
   UINavigationController
     OtherCustomViewController

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

UITabViewController
   YourCustomViewController
   OtherCustomViewController

self.navigationController可能不是nil,因为您可能正在创建一个UINavigationController,但您 - 再次,我的猜测 - 未能将 navigationController 设置为由选项卡视图控制器处理的视图控制器。

于 2012-06-19T20:23:26.193 回答
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....
于 2012-06-20T00:55:52.783 回答