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

发布于 2024-10-22 02:19:52 字数 1556 浏览 3 评论 0原文

请接受我之前的道歉,因为我的术语可能不准确,所以我的术语可能不准确,希望您能理解我要问的是什么。

我在获取 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];
}

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

谢谢大家

Accept my early apologies as totally new to this so my terminology may not be accurate, hope you can understand what it is that i'm asking.

I am having issues getting a UIBarButtonSystemItem to take me back to a different view when pushed. I have defined a UIToolBar with a UIBarButtonSystemItem on it which compiles and works perfectly. However, when the button is pushed the app crashes.

This is the code i have used to define the UIToolBar and 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:)];

I have then added flexitem to this and added the button to the array using the following code:

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

This is where I fall down and I have attempted to get the button to take me to a previous view when pushed by using the following code:

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

Like I said, this just crashes my simulator. It compiles without issue but Im guessing the code is fundamentally wrong as i am new to this. Any help explained dumbed down would be fantastic ;-)

Thanks All

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

◇流星雨 2024-10-29 02:19:52

这:

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

This:

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

should be:

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

For the (void)pressbutton code it should now be:

-(IBAction)pressButton1:(id)sender {
    BMR_TDEE_CalculatorViewController *controller = [[BMR_TDEE_CalculatorViewController alloc] initWithNibName:@"BMR_TDEE_CalculatorViewController" bundle:nil];
    [self.navigationController presentModalViewController:controller animated:TRUE];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文