How to add a UIToolbar to a UITableViewController programmatically?

后端 未结 3 747

I have opted to use a UITableViewController without a nib. I need a UIToolbar at the bottom with two buttons. What is the simplest way of doing that?

P.

相关标签:
3条回答
  • 2020-12-13 09:45

    Here is a simple example, which may help

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

    Thanks, prodeveloper

    0 讨论(0)
  • 2020-12-13 09:51

    You also can just check "shows toolbar" option in NavigationController attributes inspector.

    0 讨论(0)
  • 2020-12-13 10:00

    The simpler thing to do is to build your project on top of a UINavigationController. It already has a toolbar, it's just hidden by default. You can reveal it by toggling the toolbarHidden property, and your table view controller will be able to use it as long as it's in the navigation controller hierarchy.

    In your app delegate, or in the object your app delegate passes control to, create the navigation controller with your UITableViewController as the root view controller:

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

    Then set the toolbar items in your MyTableViewController object:

    - ( 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 ... */
    }
    
    0 讨论(0)
提交回复
热议问题