#import <Foundation/Foundation.h>
#import <UIKit/UIKitDefines.h>
#import <UIKit/UITextField.h>
#import <UIKit/UIView.h>
typedef NS_ENUM(NSInteger, UIAlertViewStyle) {
UIAlertViewStyleDefault = 0,
UIAlertViewStyleSecureTextInput,
UIAlertViewStylePlainTextInput,
UIAlertViewStyleLoginAndPasswordInput
};
@protocol UIAlertViewDelegate; 遵守的协议
@class UILabel, UIToolbar, UITabBar, UIWindow, UIBarButtonItem, UIPopoverController;
NS_CLASS_AVAILABLE_IOS(2_0) @interface UIAlertView : UIView
- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id/*<UIAlertViewDelegate>*/)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATIONNS_EXTENSION_UNAVAILABLE_IOS("Use UIAlertController instead.");
@property(nonatomic,assign) id /*<UIAlertViewDelegate>*/ delegate; // weak reference
一般用于初始化的时候直接赋值
@property(nonatomic,copy) NSString *title;
@property(nonatomic,copy) NSString *message;
当一个view中有多个alertView时,用tag值区分
alert.tag=0
添加按钮并直接给标题
- (NSInteger)addButtonWithTitle:(NSString *)title;
指定索引的按钮标题
- (NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex;
按钮总数
@property(nonatomic,readonly) NSInteger numberOfButtons;
取消按钮的索引值
@property(nonatomic) NSInteger cancelButtonIndex;
获取其他按钮的索引值
@property(nonatomic,readonly) NSInteger firstOtherButtonIndex;
@property(nonatomic,readonly,getter=isVisible) BOOL visible;
显示弹出的方法,不掉用,则不显示整个alertview控件
- (void)show;
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated;
alert显示的样式,默认的样式为UIAlertViewStyleDefault(一共有4个样式,在上面typedef NS_ENUM)
@property(nonatomic,assign) UIAlertViewStyle alertViewStyle NS_AVAILABLE_IOS(5_0);
/* Retrieve a text field at an index - raises NSRangeException when textFieldIndex is out-of-bounds.
The field at index 0 will be the first text field (the single field or the login field), the field at index 1 will be the password field.
/ *检索文本字段索引,提高NSRangeException textFieldIndex时是禁止入内的。
在索引0将成为第一个文本字段(单一字段或登录字段),在指数1将密码字段。* / */
- (UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex NS_AVAILABLE_IOS(5_0);
@end
@protocol UIAlertViewDelegate <NSObject>
@optional
选中的那个button处理点击事件,传递的参数是button的索引值
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
- (void)alertViewCancel:(UIAlertView *)alertView; 取消按钮的事件
- (void)willPresentAlertView:(UIAlertView *)alertView; 即将显示时的事件
- (void)didPresentAlertView:(UIAlertView *)alertView; 已经显示时的事件
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex; 即将消失时执行的事件
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:
(NSInteger)buttonIndex; 已经消失时执行的事件
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView;
@end
基于UITableView
//当选中某行,所触发的方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
// 屏幕上弹出的提示框(可以不遵守UIAlertViewDelegate协议也能使用)
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"这是个UIAlertView"message:@"啦啦" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消",nil];
alert.tag=100;
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex==0) {
NSLog(@"点击的是第一个按钮");//可以任意写想要的事件
}
else if (buttonIndex==1){
NSLog(@"点击的是第二个按钮");
}
else if (buttonIndex==2){
NSLog(@"点击的是第三个按钮");
}
}