iOS开发之基础视图—— UIAlertView

     UIAlertView是iOS系统自带的弹出式对话框。继承UIView。

          alertView的alertViewStyle属性:

             UIAlertViewStyleDefault 只弹信息和按钮
             UIAlertViewStyleSecureTextInput 有一个textfield加密框
             UIAlertViewStylePlainTextInput 有一个不加密的textfield
             UIAlertViewStyleLoginAndPasswordInput 有两个textfield,Login和password

        UIAlertViewDelegate方法:

               #pragma marks -- UIAlertViewDelegate --  
              //根据被点击按钮的索引处理点击事件  
              -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  
             {  
                      NSLog(@"clickButtonAtIndex:%d",buttonIndex);  
             }  

       //AlertView已经消失时执行的事件  
            -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex  
           {  
                   NSLog(@"didDismissWithButtonIndex");  
           }  
  
      //ALertView即将消失时的事件  
          -(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex  
          {  
                 NSLog(@"willDismissWithButtonIndex");  
          }  
  
      //AlertView的取消按钮的事件  
         -(void)alertViewCancel:(UIAlertView *)alertView  
         {  
                NSLog(@"alertViewCancel");  
         }  
  
     //AlertView已经显示时的事件  
        -(void)didPresentAlertView:(UIAlertView *)alertView  
       {  
               NSLog(@"didPresentAlertView");  
       }  
  
    //AlertView即将显示时  
      -(void)willPresentAlertView:(UIAlertView *)alertView  
    {  
              NSLog(@"willPresentAlertView");  

    }

     第一个示例:


//
//  ViewController.m
//  UIAlertViewDemo
//
//  Created by Apple on 16/5/12.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 创建UIAlertView控件
    UIAlertView *alertView = [[UIAlertView alloc]
                          initWithTitle:@"提示" // 指定标题
                          message:@"你正在使用警告框!"  // 指定消息
                          delegate:self // 指定委托对象
                          cancelButtonTitle:@"确定" // 为底部的取消按钮设置标题
                          // 另外设置3个按钮
                          otherButtonTitles:@"按钮一",@"按钮二",@"按钮三",nil];
    //通过给定标题添加按钮
    [alertView addButtonWithTitle:@"addButton"];
    //按钮总数
    NSLog(@"number Of Buttons :%ld",(long)alertView.numberOfButtons);
    //获取指定索引的按钮标题
    NSLog(@"buttonTitleAtIndex1:%@",[alertView buttonTitleAtIndex:1]);
    [alertView show];
    
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSString* msg = [NSString stringWithFormat:@"您点击了第%ld个按钮"
                     , (long)buttonIndex];
    // 创建UIAlertView控件
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"提示" // 指定标题
                          message:msg  // 指定消息
                          delegate:nil
                          cancelButtonTitle:@"确定" // 为底部的取消按钮设置标题
                          // 不设置其他按钮
                          otherButtonTitles:nil];
    [alert show];
    
}

@end

  效果图如下:




       第二个示例:

//
//  ViewController.m
//  UIAlertViewInputDemo
//
//  Created by Apple on 16/5/12.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"登录"
                          message:@"请输入用户名和密码"
                          delegate:self
                          cancelButtonTitle:@"取消"
                          otherButtonTitles:@"确定" , nil];
    // 设置该警告框显示输入用户名和密码的输入框
    alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
    // 设置第2个文本框关联的键盘只是数字键盘
    [alert textFieldAtIndex:1].keyboardType = UIKeyboardTypeNumberPad;
    // 显示UIAlertView
    [alert show];
    
}

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    // cancel=0 other=1
    NSLog(@"buttonIndex = %ld",(long)buttonIndex);
    // 如果用户单击了第一个按钮
    if (buttonIndex == 1) {
        // 获取UIAlertView中第1个输入框
        UITextField* nameField = [alertView textFieldAtIndex:0];
        // 获取UIAlertView中第2个输入框
        UITextField* passField = [alertView textFieldAtIndex:1];
        // 显示用户输入的用户名和密码
        NSString* msg = [NSString stringWithFormat:
                         @"您输入的用户名为:%@,密码为:%@"
                         , nameField.text, passField.text];
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"提示"
                              message:msg
                              delegate:nil
                              cancelButtonTitle:@"确定"
                              otherButtonTitles: nil];
        // 显示UIAlertView
        [alert show];
    }
}

// 当警告框将要显示出来时激发该方法
-(void) willPresentAlertView:(UIAlertView *)alertView
{
    // 遍历UIAlertView包含的全部子控件
    for( UIView * view in alertView.subviews )
    {
        // 如果该子控件是UILabel控件
        if( [view isKindOfClass:[UILabel class]] )
        {
            UILabel* label = (UILabel*) view;
            // 将UILabel的文字对齐方式设为左对齐
            label.textAlignment = NSTextAlignmentLeft;
        }
    }
}

@end

   效果图如下:




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Title: iOS 8 Programming with Swift, 2nd Edition Author: Kevin Lyn. Length: 100 pages Edition: 2 Language: English Publication Date: 2015-04-24 ISBN-10: B00WO7FVDS Book Download iOS 8 Programming with Swift: A Step By Step Guide Book for Beginners. Create Your Own App in One Day! Recently, Apple Inc released iOS 8 into the market. This followed a release of the Swift programming language for the purpose of creating apps to run on devices supporting this version of iOS. This language has shown a number of improvements in terms of functionality compared to the Objective-C programming language. iOS 8 has shown improved support for amazing features which were not supported in iOS 7. This explains the need for the creation of iOS 8 apps, and therefore the need to learn Swift. Here is a preview of what you'll learn: Getting Started with Swift Variables and Constants Type Annotations Using Objective-C Classes Animations Creating an Audio Player and so on… Table of Contents Chapter 1: Definition Chapter 2: Getting Started with iOS 8 Swift Programming Chapter 3: Variables and Constants in Swift Chapter 4: Type Annotations Chapter 5: Tuples in Swift Chapter 6: Enumerations in Swift Chapter 7: UIAlertView in Swift Chapter 8: NSTimer in Swift Chapter 9: Swift and PHP Chapter 10: Connecting to iTunes Search API Chapter 11: Image Loading and Caching Asynchronously Chapter 12: Using Objective-C classes in Swift Chapter 13: Animations in Swift Chapter 14: Creating an Audio Player with Swift

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值