在导航栏上添加两个以上的按钮 [英] adding more than two button on the navigationbar

查看:188
本文介绍了在导航栏上添加两个以上的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试,但不起作用。

I am trying this but it does not work.

-(void)viewDidLoad
{
    // create a toolbar where we can place some buttons
    UIToolbar* toolbar = [[UIToolbar alloc]
                    initWithFrame:CGRectMake(0, 0, 100, 45)];
    [toolbar setBarStyle: UIBarStyleBlackOpaque];

    // create an array for the buttons
    NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];

    // create a standard save button
    UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
        initWithBarButtonSystemItem:UIBarButtonSystemItemSave
        target:self
        action:@selector(saveAction:)];
    saveButton.style = UIBarButtonItemStyleBordered;
    [buttons addObject:saveButton];
    [saveButton release];

    // create a spacer between the buttons
    UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
        initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
        target:nil
        action:nil];
    [buttons addObject:spacer];
    [spacer release];

    // create a standard delete button with the trash icon
    UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc]
        initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
        target:self
        action:@selector(deleteAction:)];
    deleteButton.style = UIBarButtonItemStyleBordered;
    [buttons addObject:deleteButton];
    [deleteButton release];

    // put the buttons in the toolbar and release them
    [toolbar setItems:buttons animated:NO];
    [buttons release];

    // place the toolbar into the navigation bar
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
            initWithCustomView:toolbar];
    [toolbar release];
}

如何解决这个问题?

推荐答案

UIToolbar* toolbar = [[UIToolbar alloc]
                      initWithFrame:CGRectMake(0, 0, 320, 45)];
[toolbar setBarStyle: UIBarStyleBlackOpaque];

// create an array for the buttons
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:5];

// create a standard save button
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
                               initWithBarButtonSystemItem:UIBarButtonSystemItemSave
                               target:self
                               action:@selector(deleteAction:)];
saveButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:saveButton];
[saveButton release];

// create a standard delete button with the trash icon
UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc]
                                 initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
                                 target:self
                                 action:@selector(deleteAction:)];
deleteButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:deleteButton];
[deleteButton release];

UIBarButtonItem *addbutton = [[UIBarButtonItem alloc]
                                 initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                 target:self
                                 action:@selector(deleteAction:)];
addbutton.style = UIBarButtonItemStyleBordered;
[buttons addObject:addbutton];
[addbutton release];

UIBarButtonItem *editButton = [[UIBarButtonItem alloc]
                              initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
                              target:self
                              action:@selector(deleteAction:)];
editButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:editButton];
[editButton release];

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
                               initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                               target:self
                               action:@selector(deleteAction:)];
doneButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:doneButton];
[doneButton release];

// put the buttons in the toolbar and release them
[toolbar setItems:buttons animated:NO];
[buttons release];

// place the toolbar into the navigation bar
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar];
[toolbar release];

尝试此代码段

并且在AppDelegate.h文件中你声明这个

And In AppDelegate.h file you declare this

UINavigationController *navigationController; 

和AppDelegate.m文件

And AppDelegate.m file

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

// Override point for customization after application launch.

// Add the view controller's view to the window and display.
navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];

return YES;
}

这篇关于在导航栏上添加两个以上的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆