目前我使用以下代码来呈现UIAlertView:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Today's Entry Complete"
message:@"Press OK to submit your data!"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
如何获取它以便在按下“确定”时触发一个函数,比如-(void)submitData
答案 0 :(得分:42)
注意:强>
重要提示: UIAlertView在iOS 8中已弃用。(请注意,UIAlertViewDelegate也已弃用。)要在iOS 8及更高版本中创建和管理警报,请使用UIAlertController,其优先级为UIAlertControllerStyleAlert。
Please check this out tutorial
Objectvie C
.h文件
@interface urViewController : UIViewController <UIAlertViewDelegate> {
.m文件
// Create Alert and set the delegate to listen events
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Today's Entry Complete"
message:@"Press OK to submit your data!"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil];
// Set the tag to alert unique among the other alerts.
// So that you can find out later, which alert we are handling
alert.tag = 100;
[alert show];
//[alert release];
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
// Is this my Alert View?
if (alertView.tag == 100) {
//Yes
// You need to compare 'buttonIndex' & 0 to other value(1,2,3) if u have more buttons.
// Then u can check which button was pressed.
if (buttonIndex == 0) {// 1st Other Button
[self submitData];
}
else if (buttonIndex == 1) {// 2nd Other Button
}
}
else {
//No
// Other Alert View
}
}
夫特
Swifty的方法是使用新的UIAlertController和闭包:
// Create the alert controller
let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert)
// Create the actions
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
UIAlertAction in
NSLog("OK Pressed")
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) {
UIAlertAction in
NSLog("Cancel Pressed")
}
// Add the actions
alertController.addAction(okAction)
alertController.addAction(cancelAction)
// Present the controller
self.presentViewController(alertController, animated: true, completion: nil)
答案 1 :(得分:8)
如果您使用的是未在类接口中声明的多个UIAlertView实例,您还可以设置标记以标识委托方法中的实例,例如:
在你的类文件myClass.m
之上的某个地方#define myAlertViewsTag 0
创建UIAlertView:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"My Alert"
message:@"please press ok or cancel"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil];
alert.tag = myAlertViewsTag;
[alert show];
[alert release];
委托方法:
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (alertView.tag == myAlertViewsTag) {
if (buttonIndex == 0) {
// Do something when cancel pressed
} else {
// Do something for ok
}
} else {
// Do something with responses from other alertViews
}
}
答案 2 :(得分:1)
您需要在分配alertview时设置委托,然后使用其中一个UIAlertViewDelegate方法调用您自己的方法,例如:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Today's Entry Complete"
message:@"Press OK to submit your data!"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
[self submitData];
}
答案 3 :(得分:1)
在显示之前,您需要为delegate
设置UIAlertView
。然后在委托回调中完成工作:
-(void)alertView:(UIAlertView*)alert didDismissWithButtonIndex:(NSInteger)buttonIndex;
{
if ([[alert buttonTitleAtIndex] isEqualToString:@"Do it"]) {
// Code to execute on Do it button selection.
}
}
https://github.com/Jayway/CWUIKit上的我的CWUIKit项目增加了UIAlertView
,允许您使用块执行相同的操作。重复使用相同的操作来创建,显示和处理警报:
[[UIAlertView alertViewWithTitle:@"My Title"
message:@"The Message"
cancelButtonTitle:@"Cancel"
otherTitlesAndAuxiliaryActions:@"Do it",
^(CWAuxiliaryAction*a) {
// Code to execute on Do it button selection.
}, nil] show];
答案 4 :(得分:0)
如果您想使用块,即使对于多个UIAlertView,您也可以使用MKAdditions轻松实现此目的。
只需使用与此示例类似的代码:
[[UIAlertView alertViewWithTitle:@"Test"
message:@"Hello World"
cancelButtonTitle:@"Dismiss"
otherButtonTitles:[NSArray arrayWithObjects:@"First", @"Second", nil]
onDismiss:^(int buttonIndex)
{
NSLog(@"%d", buttonIndex);
}
onCancel:^()
{
NSLog(@"Cancelled");
}
] show];
您可以在本教程中找到更多信息:http://blog.mugunthkumar.com/coding/ios-code-block-based-uialertview-and-uiactionsheet
答案 5 :(得分:0)
稍微澄清,
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//handles title you've added for cancelButtonTitle
if(buttonIndex == [alertView cancelButtonIndex]) {
//do stuff
}else{
//handles titles you've added for otherButtonTitles
if(buttonIndex == 1) {
// do something else
}
else if(buttonIndex == 2) {
// do different thing
}
}
}
实施例,
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Need your action!"
message:@"Choose an option to continue!" delegate:self cancelButtonTitle:@"Not Need!"
otherButtonTitles:@"Do Something", @"Do Different", nil];
[alert show];
(这是iOS7截图)
答案 6 :(得分:0)
从iOS8 Apple提供新的UIAlertController
类,您可以使用它而不是现在已弃用的UIAlertView,它也在折旧消息中说明
UIAlertView已弃用。将UIAlertController与preferredStyle一起使用 取而代之的是UIAlertControllerStyleAlert
所以你应该使用这样的东西
目标C
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:@"Title"
message:@"Message"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:@"Yes, please"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//Handle your yes please button action here
}];
UIAlertAction* noButton = [UIAlertAction
actionWithTitle:@"No, thanks"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//Handle no, thanks button
}];
[alert addAction:yesButton];
[alert addAction:noButton];
[self presentViewController:alert animated:YES completion:nil];
夫特
Swifty的方法是使用新的UIAlertController和闭包:
// Create the alert controller
let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert)
// Create the actions
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
UIAlertAction in
NSLog("OK Pressed")
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) {
UIAlertAction in
NSLog("Cancel Pressed")
}
// Add the actions
alertController.addAction(okAction)
alertController.addAction(cancelAction)
// Present the controller
self.presentViewController(alertController, animated: true, completion: nil)