`
shuai1234
  • 浏览: 926766 次
  • 性别: Icon_minigender_1
  • 来自: 山西
社区版块
存档分类
最新评论

Iphone开发(六)IOS中的通知--操作表ActionSheet和警报AlertView

 
阅读更多

今天介绍两种控件,用来向用户提供通知并供选择,ActionSheet从底部弹出,可以显示一系列的按钮,AlertView是屏幕中间弹出一个对话框形式的,类似于android的dialog,并且可以在AlertView上添加一个TextField用来输入密码或者别的内容。具体看代码,我们还打开昨天的代码,上面我们昨天设置了一个button控件,今天我们先用这个button控件来响应一个ActionSheet:

昨天的button是空白的,我们先打开xib文件,编辑title为“点击调出ActionSheet",然后在viewController中创建一个操作方法clickButton,将该button的touch up inside行为与该方法相关联,然后在该方法中如下编辑:

 

[plain] view plaincopy
 
  1. - (IBAction)clickButton:(id)sender {  
  2.     UIActionSheet *myActionSheet=[[UIActionSheet alloc]initWithTitle:@"标题" delegate:self cancelButtonTitle:@"取消键" destructiveButtonTitle:@"毁灭键" otherButtonTitles:@"额外加键", nil];  
  3.     //这样就创建了一个UIActionSheet对象,如果要多加按钮的话在nil前面直接加就行,记得用逗号隔开。  
  4.     //下面是显示,注意ActioinSheet是出现在底部的,是附加在当前的View上的,所以我们用showInView方法  
  5.     [myActionSheet showInView:self.view];  
  6.       
  7. }  


这时,ActionSheet已经可以显示出效果了:

 

但是这个ActionSheet是没有按键响应的,如果细心的话会发现上面我们在创建ActionSheet对象时有一个delegate参数我们给了self,这个参数就是指要响应ActionSheet选择事件的对象,响应ActionSheet事件需要实现一个协议UIActionSheetDelegate,我们写了self,那么我们就在viewController里实现这个协议,然后重写这个协议中的actionSheet:didDismissWithButtonIndex:方法,用该方法来捕捉actionSheet的响应事件。看代码:

在viewController.h声明时加上这样

 

[plain] view plaincopy
 
  1. @interface ViewController : UIViewController<UIActionSheetDelegate>  


在.m文件里面添加协议方法:

 

 

[plain] view plaincopy
 
  1. -(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex  
  2. {  
  3.     if (buttonIndex==[actionSheet destructiveButtonIndex])   
  4.         //这里捕捉“毁灭键”,其实该键的index是0,从上到下从0开始,称之为毁灭是因为是红的  
  5.     {  
  6.         //点击该键后我们再弹出一个AlertView  
  7.         UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"AlertView的标题" message:@"我是一个AlertView" delegate:self cancelButtonTitle:@"取消键" otherButtonTitles:@"随手加", nil];  
  8.         //这是弹出的一个与当前View无关的,所以显示不用showIn,直接show  
  9.         [myAlertView show];  
  10.     }  
  11. }  


该方法捕捉了我们点击ActionSheet中的毁灭键后的事件:生成了一个AlertView并显示,如下

 

我们发现AlertView的生成方法和ActionSheet真的很像,不同的只是一个是show,一个是showIn,代码中我们的AlertView的两个按键我们还没有进行处理,同ActionSheet的按键响应几乎一样,我们需要将当前的类(self)实现一个协议,<UIAlertViewDelegate>

 

[plain] view plaincopy
 
  1. @interface ViewController : UIViewController<UIActionSheetDelegate,UIAlertViewDelegate>  

在.m文件中的响应方法为:

 

 

[plain] view plaincopy
 
  1. -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex  
  2. {  
  3.     //这里你自己补充吧,你可以再弹出一个ActionSheet或者什么,whatever,who cares?  
  4. }  


好了,到此为止吧,额,今天发现我的一篇Iphone系列的博文访问量飙升,原来是上首页了,信心满满的想去百度一下关键字看能不能找到,没想到搜索到了一模一样的,却是在什么红黑联盟网站的,标题和内容一模一样,唯一不同的是图片被打上了他们的logo,很感慨啊,也不知道该高兴还是该苦笑。以后要加上标注,提醒转载注明出处吧,加油。

 

 

关键字:Iphone 开发 ,Iphone SDK ,基础 ,通知 ,ActionSheet ,AlertView .

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics