ios学习笔记(三)UISlider与UISwitch控件

1 首先我们还是创建一个Single View Application,然后打开MainStoryboard_iphone.storyboard,在IB中添加一个UISlider控件和一个Label,这个Label用来显示Slider的值。

选中新加的Slider控件,打开Attribute Inspector,修改属性值,设置最小值为0,最大值为100,当前值为0.5,并确保勾选上Continuous,如下图:

接着我们放上UISwitch控件,就是很像开关的那种控件,它只有两个状态:on和off,全都放上去效果就是这样的:



2.好了我们开始写代码喽:ViewController.h:

  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController{  
  4.     UILabel *sliderlabel;  
  5.     UISwitch *leftSwitch;  
  6.     UISwitch *rightSwitch;  
  7. }  
  8.   
  9. @property (nonatomic,retain) IBOutlet UILabel *sliderlabel;  
  10. @property (nonatomic,retain) IBOutlet UISwitch *leftSwitch;  
  11. @property (nonatomic,retain) IBOutlet UISwitch *rightSwitch;  
  12.   
  13. - (IBAction)sliderChanged:(id)sender;  
  14. - (IBAction)switchChanged:(id)sender;  
  15. @end  

接着是实现 ViewController.m:

  1. #import "ViewController.h"  
  2.   
  3. @interface ViewController ()  
  4.   
  5. @end  
  6.   
  7. @implementation ViewController  
  8. @synthesize sliderlabel;  
  9. @synthesize leftSwitch;  
  10. @synthesize rightSwitch;  
  11.   
  12. - (void)viewDidLoad  
  13. {  
  14.     [super viewDidLoad];  
  15.     // Do any additional setup after loading the view, typically from a nib.  
  16. }  
  17.   
  18. - (void)viewDidUnload  
  19. {  
  20.     [super viewDidUnload];  
  21.     // Release any retained subviews of the main view.  
  22. }  
  23.   
  24. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  25. {  
  26.     if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {  
  27.         return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);  
  28.     } else {  
  29.         return YES;  
  30.     }  
  31. }  
  32.   
  33. - (IBAction)sliderChanged:(id)sender{  
  34.     UISlider *slider=(UISlider*)sender;  
  35.     int progressAsInt=(int)(slider.value+0.5f);  
  36.     NSString *newText=[[NSString alloc] initWithFormat:@"%d",progressAsInt];  
  37.     sliderlabel.text=newText;  
  38.     [newText release];  
  39.     NSLog(@"%d",progressAsInt);  
  40. }  
  41.   
  42.   
  43. - (IBAction)switchChanged:(id)sender{  
  44.     UISwitch *whichSwich=(UISwitch *)sender;  
  45.     BOOL setting=whichSwich.isOn;  
  46.     [leftSwitch setOn:setting animated:YES];  
  47.     [rightSwitch setOn:setting animated:YES];  
  48. }  
  49.   
  50. - (void)dealloc{  
  51.     [sliderlabel release];  
  52.     [leftSwitch release];  
  53.     [rightSwitch release];  
  54.     [super dealloc];  
  55. }  
  56. @end  

3.剩下的就是连接操作和输出口:

将slider控件的value changed事件与sliderChanged方法连接在一起,将swich控件的value changed事件与swichChanged方法连接在一起,当然还要把lable控件和swich控件的输出与ViewController的相应控件接口连接在一起。

最终实现的效果如下面两张图:                                                          


            

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值