2012年11月13日 星期二

UIAlertView的基本使用(二)

如果要讓UIAlertView發揮更多的功能,就要注意UIAlertViewDelegate的使用

整理相關資料如下

基本的UIAlertViewDelegate參考網頁
http://osbo.com/xcode/uialertviewdelegate/

官方文件網頁
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html


參考做法
1. ViewController.h // 紅色部分需要加上,但實作時似乎又可以不加
此處可參考網頁說明
http://stackoverflow.com/questions/10639886/why-do-i-not-need-to-declare-uialertviewdelegate-in-the-header

@interface ViewController : UIViewController <UIAlertViewDelegate>

@end


2. ViewController.m 


#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];

[[UIApplication sharedApplication] setStatusBarHidden:YES];

//四種基本的UIAlertView框架模式,要注意delegate要設成self,才能用到後面預設的Function call
//* mode1
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Other1", @"Other2", nil];
[alertView addButtonWithTitle:@"Add"];
[alertView show];
//*/


UIAlertViewStyleDefault


/* mode2
UIAlertView *alertView = [[UIAlertView alloc] init];
alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
alertView.delegate = self;
[alertView addButtonWithTitle:@"Add"];
[alertView show];
//*/


UIAlertViewStyleLoginAndPasswordInput
 


/* mode3
UIAlertView *alertView = [[UIAlertView alloc] init];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
alertView.delegate = self;
[alertView addButtonWithTitle:@"Add"];
[alertView show];
//*/


UIAlertViewStylePlainTextInput




/* mode4
UIAlertView *alertView = [[UIAlertView alloc] init];
alertView.alertViewStyle = UIAlertViewStyleSecureTextInput;
alertView.delegate = self;
[alertView addButtonWithTitle:@"Add"];
[alertView show];
//*/

UIAlertViewStyleSecureTextInput

}


- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
NSLog(@"alertViewShouldEnableFirstOtherButton");

return YES;
}
- (void)willPresentAlertView:(UIAlertView *)alertView
{
NSLog(@"willPresentAlertView");
}
- (void)didPresentAlertView:(UIAlertView *)alertView
{
NSLog(@"didPresentAlertView");
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"clickedButtonAtIndex");

switch (buttonIndex)
{
case 0:
NSLog(@"0");
break;
case 1:
NSLog(@"1");
break;
case 2:
NSLog(@"2");
break;
case 3:
NSLog(@"3");
break;
default:
break;
}
}
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSLog(@"willDismissWithButtonIndex");

switch (buttonIndex)
{
case 0:
NSLog(@"0");
break;
case 1:
NSLog(@"1");
break;
case 2:
NSLog(@"2");
break;
case 3:
NSLog(@"3");
break;
default:
break;
}
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSLog(@"didDismissWithButtonIndex");

switch (buttonIndex)
{
case 0:
NSLog(@"0");
break;
case 1:
NSLog(@"1");
break;
case 2:
NSLog(@"2");
break;
case 3:
NSLog(@"3");
break;
default:
break;
}
}
- (void)alertViewCancel:(UIAlertView *)alertView
{
NSLog(@"alertViewCancel - system canceled alert view");
}

@end

沒有留言: