gpt4 book ai didi

ios - 如何在 UIToolBar 上获取 UIBarButtonSystemItem 以将我带到另一个 View

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:57:20 25 4
gpt4 key购买 nike

请接受我早期的道歉,因为我对这个问题是全新的,所以我的术语可能不准确,希望你能理解我在问什么。

我在获取 UIBarButtonSystemItem 以在按下时将我带回不同 View 时遇到问题。我定义了一个带有 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];

这是我摔倒的地方,我试图通过使用以下代码让按钮在按下时将我带到上一个 View :

-(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];
}

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

谢谢大家

最佳答案

这个:

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];
}

关于ios - 如何在 UIToolBar 上获取 UIBarButtonSystemItem 以将我带到另一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5304748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com