• 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的文字,并且弹出对话框

    #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
    

    运行效果如下:

     

     

     

     

     

  • 相关阅读:
    设计模式学习笔记——单例(Singleton)模式
    设计模式学习笔记——抽象工厂(Abstract Factory)模式
    一些C++的好书
    C++概念重载、覆盖、隐藏
    集合的子集和集合的全排列问题
    百度二面,悲剧了,附面试题,欢迎探讨。
    【翻译】ASP.NET MVC4 入门(二)添加一个Controller
    遇到问题应该多思考一下——由一个泛型方法想到的
    【翻译】ASP.NET MVC4 入门(四)添加一个Model
    我的第一篇博客——Delegate的秘密
  • 原文地址:https://www.cnblogs.com/xinye/p/2830156.html
Copyright © 2020-2023  润新知