2009-06-19 101 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]; 

} 
工作