在动作表单中我们用很多按钮实现,在这里我们用到了UIActionsheetDelegate协议中的actionSheet:clickedButtonAtIndex:方法实现,其语法形式如下:
- (void)actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
其中,(UIActionSheet*)actionSheet 用来指定动作表单中包含的按钮,(NSInteger)buttonIndex用来指定被点击按钮的索引
核心代码如下:
ViewController.h
#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UIActionSheetDelegate>{ 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 = @"春子(はるこ)"; } }
时间: 2024-12-11 12:58:12