添加一个leftBarButtonItem崩溃

时间:2012-03-01 20:24:39

标签: iphone objective-c ios ipad

所以我试图通过以下代码将我的leftBarButtonItem设置为我的导航项:

  UIBarButtonItem * leftSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    self.navigationItem.hidesBackButton = YES;
    leftSpacer.width = 10;
    self.navigationItem.leftBarButtonItem = leftSpacer;

然而,它给了我一个收到SIGABRT的程序。这是为什么?

2 个答案:

答案 0 :(得分:0)

使用一些可能对您有帮助的代码。

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

    // create a standard "add" button
    UIBarButtonItem* bi = [[UIBarButtonItem alloc]
                           initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:NULL];
    bi.style = UIBarButtonItemStyleBordered;
    [buttons addObject:bi];

    // create a spacer
    bi = [[UIBarButtonItem alloc]
          initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    [buttons addObject:bi];

    // create a standard "refresh" button
    bi = [[UIBarButtonItem alloc]
          initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh:)];
    bi.style = UIBarButtonItemStyleBordered;
    [buttons addObject:bi];

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

    // and put the toolbar in the nav bar
    self.navigationItem.LeftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tb];

答案 1 :(得分:0)

您的代码示例显示您已分配并初始化栏按钮项,但您没有显示任何版本。你发布了吗? [leftSpacer release]