我希望有人可以帮我解决这个问题......我是初学者Xcode / Objective-C程序员。我正在开发一个上学期延续的应用程序。
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];
}
我如何/在何处放置此功能?它是在按钮属性中吗?或者我会在自定义类/控制器中编写它并将其链接到它?
2:如何让它听取警报返回: - alertView:didDismissWithButtonIndex:
3:从那里,我如何编写逻辑来隐藏页面并弹出视图?
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (alertView.tag == 1 && buttonIndex == 1) {
// Delete data and return to lobby
[self.navigationController popViewControllerAnimated:YES];
}
}
答案 0 :(得分:1)
您需要一个自定义的UIViewController来容纳按钮和警报视图交互的逻辑。我假设你知道怎么做。
完成后,假设您在视图控制器中有对按钮属性的引用,您可以以编程方式将目标添加到按钮并将选择器cancelTapped作为参数传递:
[myButton addTarget:self action:@selector(cancelTapped) forControlEvents:UIControlEventTouchUpInside];
或者,您可以控制 - 从Storyboard中的按钮拖动到自定义UIViewController的头文件,并定义IBAction。这将在您的实现中创建一个空的cancelTapped方法,然后您可以在其中添加逻辑。
至于收听UIAlertView消息,你需要通过传递" self"来使你的自定义UIViewController成为UIAlertView的委托。作为以下声明中的代表:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notification" message:@"My Message" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes"];
您的CustomViewController也应该声明为UIAlertViewDelegate。
<强> CustomViewController.h 强>
@interface CustomViewController : UIViewController<UIAlertViewDelegate>
@end
希望这有帮助!
答案 1 :(得分:0)
使用VIewWillDisappear
方法导航到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];
}
更多信息&amp;&amp;还有Custom UIBArButtonItem Check Here