2011-05-11 72 views
32

我選擇在沒有筆尖的情況下使用UITableViewController。我需要一個UIToolbar,底部有兩個按鈕。做這件事最簡單的方法是什麼?如何以編程方式將UIToolbar添加到UITableViewController?

P.S.我知道我可以輕鬆使用UIViewController並添加UITableView,但是我希望整個應用程序看起來一致。

有人可以幫忙嗎?

我看到下面的例子,我不知道它的有效性:

(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]; 

} 
+0

需要指出這裏最重要的行是[工具欄sizeToFit]沒有它 - 顯示工具欄,但不接受任何用戶交互 – 2016-04-17 11:21:30

回答

78

最簡單的方式做的是建立在UINavigationController的頂級項目。它已經有一個工具欄,它只是默認隱藏。您可以通過切換toolbarHidden屬性來顯示它,只要它在導航控制器層次結構中,表格視圖控制器就可以使用它。

在應用程序委託,或者在對象應用程序委託將控制傳遞到,與UITableViewController作爲根視圖控制器創建導航控制器:

- (void)application: (UIApplication *)application 
      didFinishLaunchingWithOptions: (NSDictionary *)options 
{ 
    MyTableViewController   *tableViewController; 
    UINavigationController  *navController; 

    tableViewController = [[ MyTableViewController alloc ] 
           initWithStyle: UITableViewStylePlain ]; 
    navController = [[ UINavigationController alloc ] 
          initWithRootViewController: tableViewController ]; 
    [ tableViewController release ]; 

    /* ensure that the toolbar is visible */ 
    navController.toolbarHidden = NO; 
    self.navigationController = navController; 
    [ navController release ]; 

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

然後設置在MyTableViewController對象中的工具欄項:

- (void)viewDidLoad 
{ 
    UIBarButtonItem   *buttonItem; 

    buttonItem = [[ UIBarButtonItem alloc ] initWithTitle: @"Back" 
              style: UIBarButtonItemStyleBordered 
              target: self 
              action: @selector(goBack:) ]; 
    self.toolbarItems = [ NSArray arrayWithObject: buttonItem ]; 
    [ buttonItem release ]; 

    /* ... additional setup ... */ 
} 
+5

這是一個驚人的建議。我從來不知道UINavigationController有一個默認隱藏的工具欄。我已經在使用它,所以這是一個真正的獎勵。也感謝您花時間真正解釋一切。 – jini 2011-05-11 15:30:52

6

您還可以在NavigationController屬性檢查器中檢查「顯示工具欄」選項。

+0

但是,然後在所有視圖上打開它,最終無論如何在那些不應該使用工具欄的視圖上使用代碼。 – 2014-05-19 07:49:47

1

下面是一個簡單的例子,這可能有助於

UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
    UIBarButtonItem *trashItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(deleteMessages)]; 
    UIBarButtonItem *composeItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(composeMail)]; 
    NSArray *toolbarItems = [NSMutableArray arrayWithObjects:spaceItem, trashItem,spaceItem,composeItem,nil]; 
    self.navigationController.toolbarHidden = NO; 
    [self setToolbarItems:toolbarItems]; 

感謝, prodeveloper

相關問題