2012-03-01 74 views
0

所以我想通過下面的代碼我leftBarButtonItem設置爲我的導航項目:添加leftBarButtonItem崩潰

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

但是,它給了我收到SIGABRT的程序。爲什麼是這樣?

+0

這裏沒有錯。所以我們需要更多的背景。你在哪裏做? – calimarkus 2012-03-01 21:23:51

回答

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

我希望這使用ARC。 ;) – 2012-03-01 21:24:22

+0

是的,我認爲是。 – Floris497 2012-03-01 22:41:35

0

您的代碼示例顯示您已分配並初始化欄按鈕項目,但不顯示發佈。你釋放它嗎? [leftSpacer release]

+0

它是如何不釋放它崩潰的應用程序,它只會泄漏它 – adit 2012-03-01 21:56:56

+0

我不知道多少分配和釋放只有如果弧在威爾給警告和錯誤,我沒有,所以我在想這個代碼是正確的。 – Floris497 2012-03-01 22:49:46