在動作表單中我們用很多按鈕實現,在這裡我們用到了UIActionsheetDelegate協議中的actionSheet:clickedButtonAtIndex:方法實現,其語法形式如下:
- (void)actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
其中,(UIActionSheet*)actionSheet 用來指定動作表單中包含的按鈕,(NSInteger)buttonIndex用來指定被點擊按鈕的索引
核心代碼如下:
ViewController.h
#import@interface ViewController : UIViewController { IBOutlet UITextView *tv; IBOutlet UILabel *l; } - (IBAction)aa:(id)sender; @end
ViewController.m
- (IBAction)aa:(id)sender { UIActionSheet *ac = [[UIActionSheet alloc]initWithTitle:@"語言選擇" delegate:self cancelButtonTitle:@"OK" destructiveButtonTitle:@"中文" otherButtonTitles:@"英文",@"日語", nil]; [ac showInView:self.view]; } - (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ NSString *t = [actionSheet buttonTitleAtIndex:buttonIndex]; if ([t isEqualToString:@"中文"]) { tv.text = @"從前有個可愛的小姑娘,誰見了都喜歡,但最喜歡她的是她的外婆,簡直是她要什麼就給她什麼。一次,外婆送給小姑娘一頂用絲絨做的小紅帽,戴在她的頭上正好合適。從此,姑娘再也不願意戴任何別的帽子,於是大家便叫她“小紅帽”。"; l.text = @"小紅帽"; }else if ([t isEqualToString:@"英文"]){ tv.text = @"Once there was a little girl, she always wore a red hat, so everybody called her the Little Red Hat.One day little red hat’s mother said to her,“Little red hat, your grandma is ill, please go and see her, give her these cakes. Remember you must walk along the main road.” “Yes, mum I remember.”She took the cakes and made her way to her grandma’s. She walked and walked. Suddenly she came across a big grey wolf. "; l.text = @"Litle Red Riding Hood"; }else if ([t isEqualToString:@"日語"]){ tv.text = @"春子(はるこ)さんの家は日本橋(にほんばし)にはありません。淺草(あさくさ)の近(ちか)くにあります。雷門(かみなりもん)はその家の西側(にしがわ)にあります。家族(かぞく)は三人です。両親(りょうしん)と彼女です。"; l.text = @"春子(はるこ)"; } }