Mais conteúdo relacionado
Semelhante a I os 05 (20)
I os 05
- 2. 內容⼤大綱
• Protocol (協定) 與 Delegate (代理)
• QV094:UITextField 的代理⽅方法
• UITextField Delegate
• QV094:UITextField 的代理⽅方法
• QV095:UITextField ⽴立即顯⽰示輸⼊入⽂文字
• QV096:UITextField 的更多控制
• UIAlertView Delegate
• QV098:處理 AlertView 上其他的按鈕
• UIActionSheet
• QV044:UIActionSheet 由下往上彈出
- 8. 協定的採⽤用
• 語法如下:
@interface 類別名稱 : 超類別 <協定名稱>
{
實體屬性的宣告;
............
}
- ⽅方法的宣告;
............ 可視為協定內的
@end ⽅方法已經宣告了
- 9. 指定必要功能與選項功能
• 例如鬧鐘的協定:
@protocol Alarm
- (void) setCurrentTime: (NSDate *) date;
- (void) alarm;
- (setAlarm: (BOOL) flag; 沒有指定的部份
@optional 等同 @required
- (BOOL) snooze;
- (void) setSnooze: (BOOL) flag;
@required
- (void) setTimerAtHour: (int)h minute:(int)m;
@end
- 12. @interface IntegerOpr : NSObject <operation>
{
main.m (2/3)
int num;
}
@property int num;
@end
(1) 指定使⽤用協定
@implementation IntegerOpr
@synthesize num;
-(void) add:(int) n
{
num += n; NSLog(@"相加後的結果為%d", num);
}
-(void) sub:(int)n
(2) 各個⽅方法實作
{
num -= n; NSLog(@"相減後的結果為%d", num);
}
-(void) mul:(int)n
{
num *= n; NSLog(@"相乘後的結果為%d", num);
}
-(void) div:(int)n
{
if(n!=0) num /= n; NSLog(@"相加後的結果為%d", num);
else NSLog(@"不能除以零");
}
@end
- 13. int main(int argc, const char * argv[]) main.m (3/3)
{
@autoreleasepool {
IntegerOpr *obj = [[IntegerOpr alloc] init];
[obj setNum:8];
NSLog(@"num初始值為 %d", [obj num]);
NSLog(@"加2......"); 主程式使⽤用協定
[obj add:2];
NSLog(@"減3......");
[obj sub:3];
NSLog(@"乘以4......");
[obj mul:4]; 執⾏行結果
NSLog(@"除以0......");
[obj div:0];
NSLog(@"除以2......");
[obj div:2];
}
return 0;
}
- 19. iOS 的代理機制
• 沒有 delegate,幾乎沒法寫程式...
UIPickerView, UIScrollView, UITextView,
UIWebView, UITableView, ......
• 程式的⽣生命週期.....AppDelegate
- 27. ⽂文字輸⼊入之後的處理事件
(2) 事件應改為
Editing Changed
(1) 事件 Value
Changed 無效
- 32. -(void)doneEdit:(id)sender ViewController.m
{
[sender resignFirstResponder];
}
-(void)checkInput:(id)sender
{
int count = [userInput.text length];
displayLabel.text = [NSString stringWithFormat:@"%d", count];
}
- (void)viewDidLoad 指定事件對應的程式
{
[super viewDidLoad];
[userInput addTarget:self action:@selector(doneEdit:)
forControlEvents:UIControlEventEditingDidEndOnExit];
[userInput addTarget:self action:@selector(checkInput:)
forControlEvents:UIControlEventEditingChanged];
//[userInput addTarget:self action:@selector(checkInput:)
forControlEvents:UIControlEventValueChanged]; //錯的
}
- 35. 實作 UITextField Delegate ViewController.m (1/2)
-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:
(NSRange)range replacementString:(NSString *)string
{
int count = [textField.text length]; // 此處直接⽤用傳⼊入物件
displayLabel.text = [NSString stringWithFormat:@"%d", count];
****** 省略部份程式 ******
return YES;
}
- 36. ViewController.m (1/2)
-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:
(NSRange)range replacementString:(NSString *)string
{
****** 省略部份程式 ******
// 可接受 backspace
if (range.length > 0 && [string length] == 0)
{
return YES; ⽂文字輸⼊入過程中,
}
// 限定字元的⻑⾧長度
可增加多項功能
int maxLength = 6;
if ([textField.text length] >= maxLength &&
![string isEqualToString:@""])
{
注意在『輸⼊入完成
return NO;
} 前執⾏行』
// 只接收指定的字元集
NSString *availableString = @"abcdef.";
if ([availableString rangeOfString:string].location == NSNotFound)
{
return NO;
}
// 不接受指定的字元
if([string isEqualToString:@"x"]) return NO;
return YES;
}
- 41. 簡單的⽤用法 ViewController.m (1/2)
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:
(NSInteger)buttonIndex
{
UIAlertView *newAlertView;
依據按鈕的索引值判斷
switch(buttonIndex)
{
case 0:
newAlertView = [[UIAlertView alloc] initWithTitle:@"答案是"
message:@"iPhone,答對了"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
break;
case 1:
newAlertView = [[UIAlertView alloc] initWithTitle:@"答案是"
message:@"Android,好吧"
delegate:nil
cancelButtonTitle:@"ok"
otherButtonTitles:nil];
break;
}
[newAlertView show];
}
- 42. 另⼀一種⽤用法。⽽而且內部還有另⼀一個 UIAlertView
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:
(NSInteger)buttonIndex
{
UIAlertView *newAlertView; ViewController.m (2/2)
if([[alertView title] isEqualToString:@"題⺫⽬目是"] )
{
switch(buttonIndex)
{ 判斷是來⾃自哪⼀一個 UIAlertView
case 0:
newAlertView = [[UIAlertView alloc] initWithTitle:@"答案是"
message:@"iPhone,答對了" delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles:nil];
break;
case 1:
newAlertView = [[UIAlertView alloc] initWithTitle:@"其他⼿手機"
message:@"選⼀一個吧" delegate:self
cancelButtonTitle:@"Android"
otherButtonTitles:@"Windows", @"傳統⼿手機", @"未知", nil];
break;
}
} 更多的按鈕
else if ([[alertView title] isEqualToString:@"其他⼿手機"] )
{
newAlertView = [[UIAlertView alloc] initWithTitle:@"你選的是"
message:[NSString stringWithFormat:@"第 %d 項", buttonIndex]
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
}
[newAlertView show];
}
- 50. ViewController.m (1/2)
- (IBAction)openActionSheet:(id)sender
{
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:@"請選擇喜愛的項⺫⽬目"
delegate:self
cancelButtonTitle:@"返回"
destructiveButtonTitle:@"特殊紅⾊色按鈕"
otherButtonTitles:@"蘋果", @"甜甜圈", @"芒果", nil];
// 也可以透過此⽅方式新增按鈕
[actionSheet addButtonWithTitle:@"增加按鈕的⽅方法"];
// 顯⽰示於畫⾯面上
[actionSheet showInView:self.view];
}
- 51. //判斷ActionSheet按鈕事件
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:
(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
message.text = @"紅⾊色特殊按鍵"; ViewController.m (2/2)
}
else if (buttonIndex == 1)
{
message.text = @"Apple喜歡你";
}
else if (buttonIndex == 2)
{
message.text = @"Android喜歡你";
}
else if (buttonIndex == 3)
{
message.text = @"Microsoft喜歡你";
}
else
{
message.text = [NSString stringWithFormat:
@"沒有處理 buttonIndex: %d", buttonIndex];
}
}
- 52. //將按鈕的Title當作判斷的依據
NSString *title = [actionSheet buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:@"蘋果"])
{
message.text = @"Apple喜歡你";
}
else if([title isEqualToString:@"甜甜圈"]) 參考利⽤用字串做
{
message.text = @"Android喜歡你";
⽐比較的⽅方法
}
else if([title isEqualToString:@"芒果"])
{
message.text = @"Microsoft喜歡你";
}
else
{
message.text = @"沒有處理這個按鍵";
}
- 53. 很多按鈕項⺫⽬目
- (IBAction)openAnother:(id)sender
{
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:@"項⺫⽬目很多時畫⾯面還不錯"
delegate:nil
cancelButtonTitle:@"返回"
destructiveButtonTitle:@"紅⾊色"
otherButtonTitles:nil];
// 指定各個按鈕
[actionSheet addButtonWithTitle:@"AAAAAAA"];
[actionSheet addButtonWithTitle:@"BBBBBBB"];
[actionSheet addButtonWithTitle:@"CCCCCCC"];
[actionSheet addButtonWithTitle:@"DDDDDDD"];
[actionSheet addButtonWithTitle:@"EEEEEEE"];
[actionSheet addButtonWithTitle:@"FFFFFFF"];
[actionSheet addButtonWithTitle:@"GGGGGGG"];
[actionSheet addButtonWithTitle:@"HHHHHHH"];
[actionSheet addButtonWithTitle:@"XXXXXXX"];
// 顯⽰示於畫⾯面上
[actionSheet showInView:self.view];
}
- 54. 簡單的畫⾯面
- (IBAction)openCopyright:(id)sender
{
NSString *aboutString = @"版權聲明 nn也可以利⽤用這種⽅方式來製作nn";
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:aboutString
delegate:nil
cancelButtonTitle:@"返回"
destructiveButtonTitle:nil
otherButtonTitles:nil]; 指定樣式
// 顯⽰示於畫⾯面上
actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[actionSheet showInView:self.view];
}