码迷,mamicode.com
首页 > 移动开发 > 详细

IOS-UIAlertView和UIActionSheet

时间:2014-11-30 15:32:46      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:uialertview   uiactionsheet   

IOS中两大控件:UIAlertView和UIActionSheet

UIAlertView是在屏幕中央弹出一个消息框,该消息框可以用来做消息提示,也可以让用户选择不同选项。

UIActionSheet是在屏幕底端弹出一个消息框,功能类似UIAlertView,不过两者除了位置不一样外,其外观也有出入。为了能够响应UIAlertView和UIActionSheet,需要设定其代理,而对应的代理需要实现对应的协议(UIAlertViewDelegate,UIActionSheetDelegate)。实现以下两个函数

// Called when a button is clicked. The view will be automatically dismissed after this call returns
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

// Called when a button is clicked. The view will be automatically dismissed after this call returns
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;


示例代码

  • 代理实现协议
    @interface ViewController : UIViewController <UIAlertViewDelegate, UIActionSheetDelegate>

  • 主要功能代码
    -(IBAction)onTextFieldEnd:(id)sender
    {
        [_textTime resignFirstResponder];
        
    // 显示对话框
    #if 0
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"TextContend" message:_textTime.text delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Good", @"Thanks", nil];
        
        [alert show];
        [alert release];
    #else
        // 底部弹出对话框
        UIActionSheet* sheet = [[UIActionSheet alloc] initWithTitle:@"ActionSheet" delegate:self cancelButtonTitle:@"cancelButtonTitle" destructiveButtonTitle:@"destructiveButtonTitle" otherButtonTitles:@"OtherButton1", @"OtherButton2", nil];
        
        [sheet showInView:self.view];
        [sheet release];
    #endif
    }
    
    // 按钮按下时响应函数
    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        NSString* strInfo = nil;
        
        strInfo = [[NSString alloc] initWithFormat:@"你按下了【%@】按钮,第%d个按钮", [alertView buttonTitleAtIndex:buttonIndex], buttonIndex];
        UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"按钮响应代理函数" message:strInfo delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        
        [alert show];
        [alert release];
        [strInfo release];
    }
    
    // 按钮按下时响应函数
    - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        NSString* strInfo = nil;
        
        strInfo = [[NSString alloc] initWithFormat:@"你按下了【%@】按钮,第%d个按钮", [actionSheet buttonTitleAtIndex:buttonIndex], buttonIndex];
        UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"按钮响应代理函数" message:strInfo delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        
        [alert show];
        [alert release];
        [strInfo release];
    }

IOS-UIAlertView和UIActionSheet

标签:uialertview   uiactionsheet   

原文地址:http://blog.csdn.net/arbboter/article/details/41623045

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!