2011-03-14 60 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