IOS6.0 学习第2篇,弹出AlertView

NS_CLASS_AVAILABLE_IOS(2_0) @interface UIAlertView : UIView {

从UIAlertView的定义可以看出UIAlertView也是UIView的子类,并且定义在IOS2.0以上的版本。

下面对其定义进行说明,然后举一个例子说明常用用法:

- (id)initWithTitle:(NSString *)title

    message:(NSString *)message

    delegate:(id /*<UIAlertViewDelegate>*/)delegate

    cancelButtonTitle:(NSString *)cancelButtonTitle

    otherButtonTitles:(NSString *)otherButtonTitles, ... ,nil;

初始化一个UIAlertView,并且指定标题(title)、显示的文字(message)、委托对象(需要实现UIAlertViewDelegate协议。一般为self,所以这个ViewController对象需要实现这个协议)、取消按钮的文字(cancelButtonTitle)、其它按钮的显示文字(多个的话使用逗号分隔开,以nil结尾)

 

@property(nonatomic,assign) id/*<UIAlertViewDelegate>*/ delegate;    // 委托对象

@property(nonatomic,copy) NSString *title; // 标题文字

@property(nonatomic,copy) NSString *message;   // 显示的消息文本文字

- (NSInteger)addButtonWithTitle:(NSString *)title;

添加一个Button到AlertView并且指定按钮显示的文字,并且返回它的索引(从0开始,cancelButton的索引是0)

- (NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex;

返回指定索引值的按钮的显示文本

@property(nonatomic,readonly) NSInteger numberOfButtons;

返回NSAlertView中的所有的按钮的数量

@property(nonatomic) NSInteger cancelButtonIndex;

@property(nonatomic,readonly) NSInteger firstOtherButtonIndex;

@property(nonatomic,readonly,getter=isVisible) BOOL visible;

- (void)show;

显示AlertView

- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated;

隐藏按下指定索引值的按钮之后,隐藏AlertView,并制定是否启动动画效果

@property(nonatomic,assign) UIAlertViewStyle alertViewStyle NS_AVAILABLE_IOS(5_0);

- (UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex NS_AVAILABLE_IOS(5_0);

返回指定索引值的TextField ,这个API仅存在于IOS5.0以上

------------------------------------------------------------------------------------------------------------------------------------------------

下面说一下UIAlertViewDelegate协议,这个协议实现了NSObject非正式协议,没有必须实现的方法,所有的方法都是可选的

@protocol UIAlertViewDelegate <NSObject>

@optional

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

当一个指定索引的按钮被点击的时候,回调此方法,buttonIndex是按钮的索引值,从0开始 

// Called when we cancel a view (eg. the user clicks the Home button). This is not called when the user clicks the cancel button.

// If not defined in the delegate, we simulate a click in the cancel button

- (void)alertViewCancel:(UIAlertView *)alertView;

 当用户按下HOME键的时候,回调此方法,用户点击Cancel按钮的时候不会回调此方法

- (void)willPresentAlertView:(UIAlertView *)alertView;  

开始显示View的动画之前进行回调

- (void)didPresentAlertView:(UIAlertView *)alertView;

显示动画完成之后进行回调 

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex;

将要开始View隐藏动画的时候进行回调

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex; 

当View的隐藏动画结束的时候进行回调

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView;

编辑任何默认的字段添加的风格之后调用

例子,这个例子在点击按钮的时候,修改UILabel的文字,并且弹出对话框

1
2
3
4
5
6
7
8
9
10
#import <UIKit/UIKit.h>
 
@interface  XinYeViewController : UIViewController <UITextFieldDelegate,UIAlertViewDelegate>
 
@property  (weak,  nonatomic IBOutlet  UIButton *myButton;
@property  (weak,  nonatomic IBOutlet  UITextField *myTextField;
@property  (weak,  nonatomic IBOutlet  UILabel *myLabel;
- ( IBAction )clickMyButton:( id )sender;
 
@end
#import "XinYeViewController.h"
 
@interface  XinYeViewController ()
 
@end
// 点击按钮的次数
static  int  CLICK_TIMES = 1;
 
@implementation  XinYeViewController
 
@synthesize  myButton;
@synthesize  myLabel;
@synthesize  myTextField;
 
- ( void )viewDidLoad
{
     [ super  viewDidLoad];
     [myTextField becomeFirstResponder];     // 一旦进入见面就显示软键盘(myTextField获得焦点)
}
 
- ( void )didReceiveMemoryWarning
{
     [ super  didReceiveMemoryWarning];
}
 
- ( IBAction )clickMyButton:( id )sender {
     
     CLICK_TIMES += 1;
     if (CLICK_TIMES % 2 == 0){
         [myLabel setText:@ "修改后的文字,modify by xinye" ];
     } else {
         [myLabel setText:@ "不要再点了,你烦不烦啊!!!" ];
     }
     [[[UIAlertView alloc] initWithTitle:@ "这里是标题"  message:@ "这里显示的是Message信息"  delegate: self  cancelButtonTitle:@ "Cancel按钮"  otherButtonTitles:@ "OK" ,@ "Hello" ,@ "World" , nil ] show];
 
}
// 在这里处理UIAlertView中的按钮被单击的事件
-( void )alertView:(UIAlertView *)alertView clickedButtonAtIndex:( NSInteger )buttonIndex
{
     NSLog (@ "buttonIndex is : %i" ,buttonIndex);
     switch  (buttonIndex) {
         case  0:{
             
         } break ;
         case  1:{
             
         } break ;
         case  2:{
             
         } break ;
         case  3:{
             
         } break ;
         default :
             break ;
     }
}
// 当点击软键盘的Enter键的时候进行回调
-( BOOL )textFieldShouldReturn:(UITextField *)textField
{
     [myTextField resignFirstResponder];     // 让myTextField失去焦点
     return  YES ;
}
 
@end

运行效果如下:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值