use*_*142 12 iphone xcode objective-c uialertview
目前我使用以下代码来呈现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];
Run Code Online (Sandbox Code Playgroud)
如何获得它,以便当按下"确定"时,它会触发一个功能,比方说 -(void)submitData
Vij*_*com 42
注意:
重要提示:在iOS 8中不推荐使用UIAlertView.(请注意,UIAlertViewDelegate也已弃用.)要在iOS 8及更高版本中创建和管理警报,请使用UIAlertController,其优先级为UIAlertControllerStyleAlert.
对象C
.h文件
@interface urViewController : UIViewController <UIAlertViewDelegate> {
Run Code Online (Sandbox Code Playgroud)
.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
}
}
Run Code Online (Sandbox Code Playgroud)
迅速
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)
Run Code Online (Sandbox Code Playgroud)
如果您正在使用未在类接口中声明的多个UIAlertView实例,则还可以设置标记以标识委托方法中的实例,例如:
在你的类文件myClass.m之上的某个地方
#define myAlertViewsTag 0
Run Code Online (Sandbox Code Playgroud)
创建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];
Run Code Online (Sandbox Code Playgroud)
委托方法:
-(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
}
}
Run Code Online (Sandbox Code Playgroud)