• IOS复习UIActionSheet&UIAlertView


    与前面相同,新建一个Single View Application项目。配置也一样,如下图



    打开chenViewController.m

    在- (void)viewDidLoad添加方法如下

    UIAlertView *alert=[[UIAlertViewalloc]initWithTitle:@"确认上线" message:@"你真的要开始了吗" delegate:self cancelButtonTitle:@"不了"otherButtonTitles:@"真的", nil];
        [alert show];
    添加这里是程序刚启动,视图刚加载的时候运行的,如下图所示

     写到这里,我想到一见事情,但是也不重要,很多人都认为nib和xib是同一个概念,
     
     这是我刚学iOS的时候在Big nerd ranch的书籍iOS programming 3rd edition(iOS编程第三版)上看到的,原话如下(章节是1.2创建界面)

    XIB文件是XML格式的文本文件,用于保存固化后的对象,构建项目时,Xcode会将XIB文件编译成NIB文件,开发者只需维护XIB文件(XIB文件更容易处理),而应用程序使用(NIB文件体积更小,更容易解析)。

     我们点击【不了】和【真的】时候没有正确的事件,都是取消,为了有响应事件,我们在chenViewController.h添加协议<UIAlertViewDelegate>,如下

    #import <UIKit/UIKit.h>
     
    @interface chenViewController : UIViewController<UIAlertViewDelegate>
     
    @end
    然后在chenViewController.m文件中添加方法如下,这是当alert按钮被点击时发生的
    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    在方法里添加代码
    if (buttonIndex==1) {
            UIActionSheet *actionsheet=[[UIActionSheetalloc]initWithTitle:@"想回家"delegate:self cancelButtonTitle:@"返回"destructiveButtonTitle:@"回家"otherButtonTitles:@"南京", nil];
            [actionsheet showInView:self.view];
        }else if (buttonIndex==0){
            NSLog(@"回家去吧");
        }
    做了个判断,上面的cancelButtonTitle:@"不了"otherButtonTitles:@"真的"
    中按钮依次是0和1,如果还有就是2.当点击了1【真的】就会弹出一个UIActionSheet,如下



    如果是0控制台就会输出回家去吧。,这些按钮有其他事件,比如将被点击,等等很多,这个还需要以后多查看官方文档,
    。下面写一下UIActionSheet的点击事件,但是注意的是顺序。
    首先在chenViewController.h添加协议UIActionSheetDelegate,多个协议用逗号隔开
    <UIAlertViewDelegate,UIActionSheetDelegate>
    接着在chenViewController.m中添加方法
    -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
    方法里面添加代码如下
    switch (buttonIndex) {
            case 0:
                NSLog(@"回家");
                break;
            case 1:
                NSLog(@"南京");
                break;
            case 2:
                NSLog(@"返回");
                break;
            default:
                break;
        }
    这次cancelButtonTitle:@"返回"destructiveButtonTitle:@"回家"otherButtonTitles:@"南京"中按钮排序不一样,因为cancelButtonTitle:@"返回"按钮出现在最下面,所以这个按钮序号在最后,其依次是0,1,上面的顺序是2,0,1
    也就是cancelButton后面的destructiveButton是0,otherButton接着是1,cancelButton就是最后一个是2,(如果还有其他的,他就是最后一个otherbutton+1)
    如果忘记了或者不明白可以看控制台来调。


    其实这两个控件用发非常相视。

    想起一句话,学的是思想,技术天天在更新。要想学好,就得学好编程思想。
            2013年8月8日9:46东南大学无锡分校桃园3宿舍106室


    补充内容

     
    //AlertView已经消失处理的事件
     
    -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
     
    //AlertView即将消失时,处理的事件
     
    -(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
     
    //AlertView 取消时
     
    -(void)alertViewCancel:(UIAlertView *)alertView
     
    //AlertView已经显示的时候
     
    -(void)didPresentAlertView:(UIAlertView *)alertView
     
    //AlertView即将显示
     
    -(void)willPresentAlertView:(UIAlertView *)alertView
     
    - (void)viewDidLoad {
     
            UILabel *numOfBtn = [[UILabel alloc]initWithFrame:CGRectMake(10.0, 10.0, 30.0, 30.0)];
     
            UILabel *titleOfBtn = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 10.0, 100.0, 30.0)];
     
            UILabel *cancelBtnIndex = [[UILabel alloc]initWithFrame:CGRectMake(200.0, 10.0, 30.0, 30.0)];
     
            UILabel *destructiveBtnIndex = [[UILabel alloc]initWithFrame:CGRectMake(10.0, 50.0, 30.0, 30.0)];
     
            UILabel *firstOtherBtnIndex = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 50.0, 30.0, 30.0)];
     
            UIActionSheet *actionSheetTest = [[UIActionSheet alloc]initWithTitle:@"ActionSheetTest"                                                                         delegate:self                                                                         cancelButtonTitle:@"CancelButton"                                                                          destructiveButtonTitle:@"RedButton"                                                                         otherButtonTitles:@"OtherButton1",@"OtherButton2",nil];
     
            //看actionSheet是否可见,这是一个只读属性
     
            BOOL a = actionSheetTest.visible;
     
            NSLog(@"%d",a);
     
            //不考虑指定索引的按钮的动作,可以设置是否有动画
     
            [actionSheetTest dismissWithClickedButtonIndex:0 animated:NO];
     
            //设置标题
     
            actionSheetTest.title = @"ActionSheetTitle";
     
            //通过给定标题添加按钮
     
            [actionSheetTest addButtonWithTitle:@"addButtonWithTitle"];
     
            //按钮总数
     
            numOfBtn.text = [NSString stringWithFormat:@"%d",actionSheetTest.numberOfButtons];
     
            //获取指定索引的标题
     
            titleOfBtn.text = [actionSheetTest buttonTitleAtIndex:4];
     
            //获取取消按钮的索引
     
            cancelBtnIndex.text = [NSString stringWithFormat:@"%d",actionSheetTest.cancelButtonIndex];
     
            //获取红色按钮的索引
     
            destructiveBtnIndex.text = [NSString stringWithFormat:@"%d",actionSheetTest.destructiveButtonIndex];
     
            //获取第一个其他按钮的索引
     
            firstOtherBtnIndex.text = [NSString stringWithFormat:@"%d",actionSheetTest.firstOtherButtonIndex];
     
            //设置actionSheet出现的方式
     
            [actionSheetTest showInView:self.view];//or [actionSheetTest showFromTabBar:] or [actionSheetTest showFromToolBar:]
     
            [self.view addSubview:numOfBtn];
     
            [self.view addSubview:titleOfBtn];
     
            [self.view addSubview:cancelBtnIndex];
     
            [self.view addSubview:destructiveBtnIndex];
     
            [self.view addSubview:firstOtherBtnIndex];
     
            [actionSheetTest release];
     
            [numOfBtn release];
     
            [titleOfBtn release];
     
            [cancelBtnIndex release];
     
            [destructiveBtnIndex release];
     
            [firstOtherBtnIndex release];
     
            [super viewDidLoad];
     
    }
    这个方法现在已经淘汰了
     
    - (void)viewDidUnload {
     
    }
    这个方法使用arc后也不需要了,(我一直没使用,但是别人博客经常看到)
     
    - (void)dealloc {
     
            [super dealloc];
     
    }
     
    #pragma mark -- UIActionSheetDelegate --
     
    //根据被点击按钮的索引处理点击事件
     
    - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
     
            NSLog(@"clickedButtonAtIndex:%d",buttonIndex);
     
    }
     
    //ActionSheet已经消失时
     
    - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
     
            NSLog(@"didDismissWithButtonIndex:%d",buttonIndex);
     
    }
     
    //ActionSheet即将消失时
     
    - (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex {
     
            NSLog(@"willDismissWithButtonIndex:%d",buttonIndex);
     
    }
     
    //
     
    - (void)actionSheetCancel:(UIActionSheet *)actionSheet {
     
            NSLog(@"actionSheetCancel");  
     
              
     
    }  
     
    //ActionSheet已经显示时  
     
    - (void)didPresentActionSheet:(UIActionSheet *)actionSheet {  
     
            NSLog(@"didPresentActionSheet%@",actionSheet);  
     
    }  
     
    //ActionSheet即将显示时  
     
    - (void)willPresentActionSheet:(UIActionSheet *)actionSheet {  
     
            NSLog(@"willPresentActionSheet%@",actionSheet);  
     
    }  
     
     
     
    @end  
     
    - (void)viewDidLoad {
     
            //初始化AlertView
     
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"AlertViewTest"
     
                                                              message:@"message"
     
                                                              delegate:self
     
                                                              cancelButtonTitle:@"Cancel"
     
                                                              otherButtonTitles:@"OtherBtn",nil];
     
            //设置标题与信息,通常在使用frame初始化AlertView时使用
     
            alert.title = @"AlertViewTitle";
     
            alert.message = @"AlertViewMessage";
     
        
     
            //这个属性继承自UIView,当一个视图中有多个AlertView时,可以用这个属性来区分
     
            alert.tag = 0;
     
        
     
            //只读属性,看AlertView是否可见
     
            NSLog(@"%d",alert.visible);
     
        
     
            //通过给定标题添加按钮
     
            [alert addButtonWithTitle:@"addButton"];
     
        
     
            //按钮总数
     
            NSLog(@"numberOfButtons:%d",alert.numberOfButtons);
     
        
     
            //获取指定索引的按钮的标题
     
            NSLog(@"buttonTitleAtIndex:%@",[alert buttonTitleAtIndex:2]);
     
        
     
            //获得取消按钮的索引
     
            NSLog(@"cancelButtonIndex:%d",alert.cancelButtonIndex);
     
        
     
            //获得第一个其他按钮的索引
     
            NSLog(@"firstOtherButtonIndex:%d",alert.firstOtherButtonIndex);
     
        
     
            //显示AlertView
     
            [alert show];
     
        
     
            [alert release];
     
            [super viewDidLoad];
     
    }
     
    #pragma mark  -- UIAlertViewDelegate --
     
    //根据被点击按钮的索引处理点击事件
     
    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
     
            NSLog(@"clickedButtonAtIndex:%d",buttonIndex);
     
    }
     
    //AlertView已经消失时
     
    - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
     
            NSLog(@"didDismissWithButtonIndex");
     
    }
     
    //AlertView即将消失时
     
    - (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
     
            NSLog(@"willDismissWithButtonIndex");
     
    }
     
     
     
    - (void)alertViewCancel:(UIAlertView *)alertView {
     
            NSLog(@"alertViewCancel");  
     
    }  
     
    //AlertView已经显示时  
     
    - (void)didPresentAlertView:(UIAlertView *)alertView {  
     
            NSLog(@"didPresentAlertView");  
     
    }  
     
    //AlertView即将显示时  
     
    - (void)willPresentAlertView:(UIAlertView *)alertView {  
     
            NSLog(@"willPresentAlertView");  
     
    }
                   日期忘记写了

  • 相关阅读:
    ImageLightbox.js – 响应式的图片 Lightbox 插件
    精美素材:10套最新出炉的免费扁平图标下载
    盘点2013年那些最优秀的网页设计作品【系列三】
    12个带给你惊喜用户体验的手机界面设计
    一款效果精致的 jQuery 多层滑出菜单插件
    创意无限:20幅惊人的霓虹灯摄影照片欣赏
    『设计前沿』14款精致的国外 iOS7 图标设计示例
    2013年值得我们学习的网页设计作品【系列二】
    Summernote – 基于 Bootstrap 的文本编辑器
    『摄影欣赏』2013年微软必应搜索十大首页美图
  • 原文地址:https://www.cnblogs.com/ioschen/p/3248834.html
Copyright © 2020-2023  润新知