《IOS开发系列教程》对话框

《IOS开发系列教程》对话框

开发窗口程序,最长用到的一个窗口就是对话框,这在MFC还是QT,亦或者是wxWidget中,都是如此,那么IOS开发中,也提供了这个功能。

我们有2个选择

  • UIAlertView
  • UIActionSheet

这个2个都可以做到显示一些选项,让用户选择,比如yes,no之类,不过UIActionSheet的功能更加丰富一点,可以提供更多的选项给用户,比如让用户的选择使用哪种账号登陆,google,facebook,twitter。

UIAlertView UIActionSheet
iosalert iosactionsheet

 

使用UIAlertView:

 

让要显示对话框的类继承自UIAlertViewDelegate

 

比如:

 

@interface xxxViewController : UIViewController <UIAlertViewDelegate>

 

调用对话框:

 

UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Hello World!"
                                                  message:@"This is your first UIAlertview message."
                                                 delegate:self
                                        cancelButtonTitle:@"Button 1"
                                        otherButtonTitles:@"Button 2", @"Button 3", nil];
[message show];
对话框的回调函数
这个在 xxxViewController.m中实现
这个函数中检查用户选择了哪个选项
 

 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
     
    if([title isEqualToString:@"Button 1"])
    {
        NSLog(@"Button 1 was selected.");
    }
    else if([title isEqualToString:@"Button 2"])
    {
        NSLog(@"Button 2 was selected.");
    }
    else if([title isEqualToString:@"Button 3"])
    {
        NSLog(@"Button 3 was selected.");
    }
}

 

使用UIActionSheet:

 

让要显示对话框的类继承自UIActionSheetDelegate

 

比如:

 

@interface xxxViewController : UIViewController <UIActionSheetDelegate>

 

调用显示:

 

NSString *actionSheetTitle = @"Action Sheet Demo"; //Action Sheet Title
NSString *destructiveTitle = @"Destructive Button"; //Action Sheet Button Titles
NSString *other1 = @"Other Button 1";
NSString *other2 = @"Other Button 2";
NSString *other3 = @"Other Button 3";
NSString *cancelTitle = @"Cancel Button";
UIActionSheet *actionSheet = [[UIActionSheet alloc]
                              initWithTitle:actionSheetTitle
                              delegate:self
                              cancelButtonTitle:cancelTitle
                              destructiveButtonTitle:destructiveTitle
                              otherButtonTitles:other1, other2, other3, nil];
[actionSheet showInView:self.view];

 

定义回调函数并且检查用户选择的选项:

 

- (void)actionSheet:(UIActionSheet *)popup clickedButtonAtIndex:(NSInteger)buttonIndex {

  switch (popup.tag) {
    case 1: {
        switch (buttonIndex) {
            case 0:
                [self FBShare];
                break;
            case 1:
                [self TwitterShare];
                break;
            case 2:
                [self emailContent];
                break;
            case 3:
                [self saveContent];
                break;
            case 4:
                [self rateAppYes];
                break;
            default:
                break;
        }
        break;
    }
    default:
        break;
 }
}

 

版权所有,禁止转载. 如需转载,请先征得博主的同意,并且表明文章转载自:IT夜班车,否则按侵权处理.

 

    分享到:

Leave a Reply

Your email address will not be published. Required fields are marked *