2009-06-19 69 views
1

我的主控制器是UITableViewController的子类,底部有一个UIToolBar,当选择一行时,我希望显示没有工具栏的另一个视图。如何隐藏子视图中的UIToolBar?现在,它始终存在于所有子视图中,除非它们被创建为模态。为UITableViewController的子视图隐藏UIToolBar

工具栏在RootController创建:

self.toolbar = [[UIToolbar alloc] init]; 
// add tool bar items here 
[self.navigationController.view addSubview:toolbar]; 

RootController显示其子视图这样:

RootController *rootcontroller = [[RootController alloc] initWithStyle:UITableViewStyleGrouped]; 
self.navigationController = [[UINavigationController alloc] initWithRootViewController:rootcontroller]; 

[rootcontroller release]; 

[window addSubview:[self.navigationController view]]; 

UIViewController *controller = [[UIViewController alloc] init...] 
[self.navigationController pushViewController:controller animated:YES]; 

RootController在应用程序委托的applicationDidFinishLaunching实例化为这样如果我将该工具栏添加到RootControll中的[self.view]中呃而不是导航控制器的视图,工具栏消失在一起..

回答

0

另一种选择将使用 “removeFromSuperview”

[工具栏removeFromSuperview];

然后在视图中使用viewDidAppear方法,您要重新显示工具栏。 它比viewWillAppear效果更好,因为在显示视图后添加了工具栏。 (对于viewWillAppear中,工具栏在过渡期间增加所以它是有点尴尬。)

2

您可以尝试隐藏工具栏,然后在显示我们的子视图'toolbar.hidden = YES',然后在您的viewWillAppear方法,再次显示'工具栏。隐藏= NO'。

0

我得到了它这个

[toolbar removeFromSuperview]; 

入住这

- (void)viewWillAppear:(BOOL)animated 
{ 
[super viewWillAppear:animated]; 

//Initialize the toolbar 
toolbar = [[UIToolbar alloc] init]; 
toolbar.barStyle = UIBarStyleDefault; 

//Set the toolbar to fit the width of the app. 
[toolbar sizeToFit]; 

//Caclulate the height of the toolbar 
CGFloat toolbarHeight = [toolbar frame].size.height; 

//Get the bounds of the parent view 
CGRect rootViewBounds = self.parentViewController.view.bounds; 

//Get the height of the parent view. 
CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds); 

//Get the width of the parent view, 
CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds); 

//Create a rectangle for the toolbar 
CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight); 

//Reposition and resize the receiver 
[toolbar setFrame:rectArea]; 

//Create a button 
UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] 
initWithTitle:@"back" style:UIBarButtonItemStyleBordered target:self action:@selector(info_clicked:)]; 

[toolbar setItems:[NSArray arrayWithObjects:infoButton,nil]]; 

//Add the toolbar as a subview to the navigation controller. 
[self.navigationController.view addSubview:toolbar]; 



[[self tableView] reloadData]; 

} 

- (void) info_clicked:(id)sender { 


[self.navigationController popViewControllerAnimated:YES]; 
[toolbar removeFromSuperview]; 

} 
工作