2011-03-14 58 views
2

接受我早期的道歉,因爲我的術語可能不準確,希望你能理解我問的是什麼。如何讓UIToolBar上的UIBarButtonSystemItem帶我到另一個視圖

我有問題得到一個UIBarButtonSystemItem帶我回推到不同的視圖。我已經定義了一個UIToolBar和一個UIBarButtonSystemItem,它編譯和完美工作。但是,當按下按鈕時,應用程序崩潰。

這是我用來定義UIToolBar和UIButton的代碼:

//create toolbar using new 
toolbar = [UIToolbar new]; 
toolbar.barStyle = UIBarStyleBlackTranslucent; 
[toolbar sizeToFit]; 
toolbar.frame = CGRectMake(0, 410, 320, 50); 

//Add buttons 
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] 
initWithBarButtonSystemItem:UIBarButtonSystemItemAction 
        target:self 
        action:@selector(pressButton1:)]; 

然後我已經加入flexitem到這一點,並使用下面的代碼添加的按鈕的陣列:

//Use this to put space in between your toolbox buttons 
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] 
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
        target:nil 
        action:nil]; 

//Add buttons to the array 
NSArray *items = [NSArray arrayWithObjects: systemItem1, flexItem, nil]; 

這是我跌倒的地方,我試圖通過使用以下代碼將按鈕帶到前一視圖:

-(void)pressButton1:(id)sender { 
    BMR_TDEE_CalculatorViewController *controller1 = [[BMR_TDEE_CalculatorViewController alloc] initWithNibName:@"BMR_TDEE_ViewController" bundle:nil]; 

    controller1.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
    [self presentModalViewController:controller1 animated:YES]; 
    [controller1 release]; 
} 

就像我說的,這只是讓我的模擬器崩潰。它編譯沒有問題,但我猜測代碼是根本錯誤的,因爲我是新手。任何幫助解釋簡單化將是非常美妙;-)

感謝所有

+0

請問您我們如何記錄崩潰? – 2011-03-15 08:25:18

回答

1

此:

UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] 
initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(pressButton1:) 

應該是:

UIButton *button=[[UIButton alloc] init]; 
button.frame=CGRectMake(0, 0, 65, 32); 
[button addTarget:self action:@selector(pressButton1:) forControlEvents:UIControlEventTouchUpInside]; 

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button]; 

對於(void)pressbutton代碼現在應該是:

-(IBAction)pressButton1:(id)sender { 
    BMR_TDEE_CalculatorViewController *controller = [[BMR_TDEE_CalculatorViewController alloc] initWithNibName:@"BMR_TDEE_CalculatorViewController" bundle:nil]; 
    [self.navigationController presentModalViewController:controller animated:TRUE]; 
} 
+0

感謝所有的信息和編輯......但似乎沒有任何改變行爲。 – 2011-03-14 22:04:55