简体   繁体   English

如何让UIBarButton执行自定义函数?

[英]How to have a UIBarButton execute a custom function?

I'm hoping someone can help me figure this out...I'm a beginner Xcode / Objective-C programmer. 我希望有人能帮我解决这个问题...我是Xcode / Objective-C初学者。 I'm working on a app that is a continuation of last semester. 我正在开发上学期的延续应用程序。

1: So I created a button and I need it to execute this custom function: 1:所以我创建了一个按钮,需要它来执行此自定义功能:

- (void)cancelTapped {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notification" message:@"Do you want to delete everything and go back to product selection?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes"];
    [alert setTag:1];
    [alert show];
}

How/where do I put this function? 如何/在何处放置此功能? Is it in the button properties? 它在按钮属性中吗? Or would I write this in a custom class/controller and link it to it? 还是我会在自定义类/控制器中编写并链接到它?

2: How do I get it to listen for the alert to return on: - alertView:didDismissWithButtonIndex: 2:如何获取它以侦听警报返回: alertView:didDismissWithButtonIndex:

3: From there, how would I would write the logic to hide the page and pop the view? 3:从那里开始,我将如何编写逻辑来隐藏页面并弹出视图?

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 1 && buttonIndex == 1) {
        // Delete data and return to lobby
        [self.navigationController popViewControllerAnimated:YES];
    }
}

You will need a custom UIViewController to house the logic for your button and alert view interactions. 您将需要一个自定义UIViewController来容纳按钮和警报视图交互的逻辑。 I am assuming you know how to do that. 我假设您知道该怎么做。

Once done, assuming you have a reference to your button property in your view controller, you can programatically add a target to your button and pass the selector cancelTapped as a parameter: 完成后,假设您在视图控制器中具有对button属性的引用,则可以以编程方式向按钮添加目标,并将选择器cancelTapped作为参数传递:

[myButton addTarget:self action:@selector(cancelTapped) forControlEvents:UIControlEventTouchUpInside];

Alternatively you could control-drag from the button in your Storyboard to the header file of your custom UIViewController, and define an IBAction. 或者,您可以控制从情节提要中的按钮拖动到自定义UIViewController的头文件中,并定义IBAction。 That will create an empty cancelTapped method in your implementation which you could then add your logic in. 这将在实现中创建一个空的cancelTapped方法,然后可以在其中添加逻辑。

As for listening on UIAlertView messages, you will need to make your custom UIViewController a delegate of the UIAlertView by passing "self" as the delegate in the following statement: 至于侦听UIAlertView消息,您需要通过在以下语句中传递“ self”作为委托,以使自定义UIViewController成为UIAlertView的委托:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notification" message:@"My Message" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes"];

Your CustomViewController should also be declared as a UIAlertViewDelegate. 您的CustomViewController也应该声明为UIAlertViewDelegate。

CustomViewController.h CustomViewController.h

@interface CustomViewController : UIViewController<UIAlertViewDelegate>
@end

Hope this helps! 希望这可以帮助!

By the using VIewWillDisappear method to detect the press of The back button of NavigationItem: 通过使用VIewWillDisappear方法detect the press of The back button NavigationItem detect the press of The back button

-(void) viewWillDisappear:(BOOL)animated {
    if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
       // Navigation button was pressed. Do some stuff 
      [self cancelTapped];

    }
    [super viewWillDisappear:animated];
}
- (void)cancelTapped {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notification" message:@"Do you want to delete everything and go back to product selection?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes"];
    [alert setTag:1];
    [alert show];
}

For More info && also Custom UIBArButtonItem Check Here 有关更多信息,&&还自定义UIBArButtonItem 在此处检查

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM