转自

原文地址:http://blog.csdn.net/newjerryj/article/details/6446370

 

  这种switch开关,应该算是apple的又一个界面创新了。 简单直观,使用方便。 本文就来介绍下switch控件在ios中的使用。

 

  在这里,我使用插入图片开关来作为示例吧。

 

  1  从Library中拖一个switch控件在vewcontroller视图控制器上。

 

  2. 在.h 代码中定义变量

     UISwitch  *switchImage;

        

       并指明属性 

         @property(nonatomic,retainIBOutlet UISwitch  *switchImage; 

     

      动作响应函数

-(IBAction)insertImage:(UISwitch*)sender;

 

 

     在.m文件中同步下,

 

     @synthesize switchImage;

 

 

 

  3. 打开xib, 在view mode中,将File's owner中的响应函数接口和控件的Touch up inside关联起来

 

  4. 编写响应函数

 

 

   

[cpp]  view plain copy
 
  1.    -(void)insertImage:(UISwitch *)sender{  
  2. if (sender==switchImage) {  
  3.     BOOL isOn=switchImage.on;     
  4.       
  5.     if (isOn) {  
  6.         UIImagePickerController *pickController=[[UIImagePickerController alloc] init];  
  7.           
  8.         pickController.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;  
  9.           
  10.         pickController.delegate=self;  
  11.           
  12.         [self presentModalViewController:pickController animated:YES];  
  13.           
  14.         [pickController release];  
  15.           
  16.     }  
  17. }  
 

 

 

 

   5. 图片管理器的其他响应事件代码

   

[cpp]  view plain copy
 
  1. -(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info  
  2. {  
  3.     [picker dismissModalViewControllerAnimated:YES];  
  4.     UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];  
  5.     NSString *filePath=[NSTemporaryDirectory() stringByAppendingFormat:@"temp.png"];  
  6.     [UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES];  
  7.     self.fileUrl=filePath;  
  8.       
  9. }  
  10. -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker  
  11. {  
  12.       
  13.     [picker dismissModalViewControllerAnimated:YES];  
  14.       
  15.     [switchImage setOn:NO animated:YES];  
  16. }  
 

 

 

 

    ok, 示例完成。

arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()