2013-10-15 43 views
0

我是新來這...我想添加一個工具欄動態,但不知道如何訪問到視圖控制器中添加工具欄視圖

我有一個基於一個UITableView的視圖控制器 h文件

@interface OAFeaturesViewController : UITableViewController<PFLogInViewControllerDelegate, PFSignUpViewControllerDelegate> 

我可以創建/添加工具欄,但它捲起附着到當我使用的tableView

[self.view addSubview:myToolBar];

我如何獲得對主窗口的引用,以便我可以將它添加到它所屬主屏幕的底部?
.m文件

UIToolbar *myToolBar = [[UIToolbar alloc]init]; 
myToolBar.frame = CGRectMake(0,0,430, 44); 
UIBarButtonItem *takePicButton = [[UIBarButtonItem alloc] initWithTitle:@"Take Pic" style:(UIBarButtonItemStyleBordered) target:self action:@selector(takePictureWithCamera)]; 

UIBarButtonItem *cancelPicButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:(UIBarButtonItemStyleBordered) target:self action:@selector(cancelPictureWithCamera)]; 


NSArray *items = [NSArray arrayWithObjects: takePicButton, cancelPicButton, nil]; 
[myToolBar setItems:items animated:NO]; 

[self.view addSubview:myToolBar];

UIImage *toolbarBkg = [[UIImage imageNamed: @"Navbar_background_solidblue.png"] stretchableImageWithLeftCapWidth:0 topCapHeight:0]; 

if ([myToolBar respondsToSelector:@selector(setBackgroundImage:forToolbarPosition:barMetrics:)]) 
{ 
    [myToolBar setBackgroundImage:toolbarBkg forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault]; 
    NSLog(@"toolbar responds to selector"); 
} 
else { 
    NSLog(@"toolbar does NOT respond to selector"); 
    UIImageView *background = [[UIImageView alloc] initWithImage:toolbarBkg]; 
    background.frame = myToolBar.frame; 
    [myToolBar insertSubview:background atIndex:0]; 

} 


[self.view bringSubviewToFront:myToolBar]; 

回答

1

您應該使用UINavigationController的和激活工具欄:

self.navigationController.toolbarHidden=NO; 

然後,如果你想將項目添加到工具欄:

UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; 
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil]; 
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil]; 

NSArray *items = [NSArray arrayWithObjects:item1, flexibleItem, item2, nil]; 
self.toolbarItems = items; 

[flexibleItem release]; 
[item1 release]; 
[item2 release]; 

flexibleItem在創建爲了保持創建的兩個項目之間的差距。

+0

[self.navigationController.toolbar addSubView:myToolBar];解決了背景和位置,但按鈕沒有顯示出來。我想我可以弄清楚。感謝您的幫助。 – user2013871