如何在UIToolBar上获取UIBarButtonSystemItem以将我带到另一个视图

时间:2011-03-14 21:25:40

标签: ios cocoa-touch uibarbuttonitem uitoolbar

接受我的早期道歉是完全陌生的,所以我的术语可能不准确,希望你能理解我在问什么。

我遇到问题让UIBarButtonSystemItem在推送时将我带回另一个视图。我已经定义了一个带有UIBarButtonSystemItem的UIToolBar,它可以完美地编译和工作。但是,当按下按钮时,应用程序崩溃。

这是我用来定义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];
}

就像我说的,这只会让我的模拟器崩溃。它编译没有问题,但我猜测代码是根本错误的,因为我是新手。愚蠢的任何帮助都会很棒; - )

全部谢谢

1 个答案:

答案 0 :(得分: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];
}