开发者

iOS - Three buttons in Nav. Bar - not Tab Bar

I am building an app that sort of calls for three buttons in the top nav. bar. I don't want to do a tab bar, as there will be only one item in the tab.

In the code below, the left button (Edit) works fine. The right button - the link to cards also works fine. However the stupid Add button gives me an error that it cannot find the selector (insertNewObject).

If I take out my custom code and use the code that says "Works in a pinch", well.. it works.

I'd appreciate either knowing what I'm doing wrong, or else, another way of handling the problem at hand - linking to another part of the app.

TIA.

  /*
    //  THIS WORKS IN A PINCH
    // Set up the edit and add buttons.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject)];
    self.navigationItem.rightBarButtonItem = addButton;
    [addButton release];

   //  END OF WHAT WORKS
*/
    // create a toolbar to have two buttons in the right
    UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 105, 44.01)];

    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    // create the array to hold the buttons, which then gets added to the toolbar
    NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];

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

    // create a standard "add" button


    // create a flip button
    UIBarButtonItem* flipButton = [[UIBarButtonItem alloc]
          initWithTitle:@"Cards" style:UIBarButtonItemStyleBordered
          target:self action:@selector(flipToFront)]; 
    [buttons addObject:flipButton];
    [flipButton release];


    // stick the buttons in the toolbar
    [tools setItems:buttons animated:NO];

    [buttons release];

    // and put the toolbar in the nav bar

    UIBarButtonItem* rightButtonBar = [[UIBarButtonItem alloc] initWithCustomView:tools];
    self.navigationItem.rightBarButtonItem = rightButtonBar;
    [rightButtonBa开发者_Python百科r release];


You may be missing a colon after the selector name. I think just putting in @selector(newObject) is a reference to a method with no parameters. @selector(newObject:) may work instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜