簡體   English   中英

如何以編程方式將UIToolbar添加到UITableViewController?

[英]How to add a UIToolbar to a UITableViewController programmatically?

我選擇使用沒有筆尖的UITableViewController 我需要一個底部帶有兩個按鈕的UIToolbar。 這樣做最簡單的方法是什么?

PS我知道我可以輕松使用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];

}

更簡單的方法是在UINavigationController之上構建項目。 它已經有一個工具欄,默認情況下它只是隱藏了。 您可以通過切換toolbarHidden屬性來顯示它,只要它在導航控制器層次結構中,您的表視圖控制器就能夠使用它。

在您的app委托中,或者您的app委托傳遞控件的對象中,使用您的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 ... */
}

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

這是一個簡單的例子,可能有所幫助

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

謝謝,開發人員

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM