前面了解了iOS的基本知识,并且写了一个简单的app,从现在的文章开始就正式进入iOS的学习中了。先学习以下iOS的基本控件。
- (IBAction)sliderAction:(id)sender { UISlider *slider = (UISlider *)sender; int progressInt = ( int )roundf(slider.value); lableOutlet.text = [NSString stringWithFormat:@"%d",progressInt]; }
- (IBAction)controlAction:(id)sender { if ([sender selectedSegmentIndex ] == 0 ) { firstImageView.hidden = YES; secondImageview.hidden = YES; } else if ( [sender selectedSegmentIndex ] == 1){ firstImageView.hidden = NO; secondImageview.hidden = YES; } else{ firstImageView.hidden = YES; secondImageview.hidden = NO; } }
2.添加3个switch到界面上,如下:
3.添加outlet和action
为3个switch添加outlet ,3个switch共用一个action,segmented control使用一个action。如何使得3个switch共用一个action看上文。添加完成之后的.h文件如下:
#import <UIKit/UIKit.h> @interface UIExample1ViewController : UIViewController - (IBAction)sliderAction:(id)sender; @property (weak, nonatomic) IBOutlet UILabel *lableOutlet; @property (weak, nonatomic) IBOutlet UISwitch *firstSwitch; @property (weak, nonatomic) IBOutlet UISwitch *secondSwitch; @property (weak, nonatomic) IBOutlet UISwitch *thirdSwitch; - (IBAction)switchAction:(id)sender; - (IBAction)controlAction:(id)sender; @end.m文件如下:我这里没有自动添加属性,所以我都是自己写的。不知道是什么原因导致的没有自动添加。
#import "UIExample1ViewController.h" @interface UIExample1ViewController () @end @implementation UIExample1ViewController @synthesize lableOutlet; @synthesize firstSwitch; @synthesize secondSwitch; @synthesize thirdSwitch; .....- (IBAction)switchAction:(id)sender {
}
- (IBAction)controlAction:(id)sender {
}
@end
- (IBAction)switchAction:(id)sender { UISwitch * whichSwitch = (UISwitch * ) sender; BOOL seeting = whichSwitch.isOn; [ firstSwitch setOn:seeting animated:YES]; [ secondSwitch setOn:seeting animated:YES]; [ thirdSwitch setOn:seeting animated:YES]; }
运行程序可以看到,3个switch状态都是一样的,如果有一个switch改变了,其余2个也都跟着改变。
打完手工。