iOS常用小控件集合

//【UISlider】一个滑块控件,高度固定(31)-事件驱动型控件
UISlider *sl = [[UISlider alloc] initWithFrame:CGRectMake(10,74,300,50)];
 //设置最大最小值
 sl.minimumValue = 0.0;
 sl.maximumValue = 100.0;
 //设置滑块当前指示的值
 sl.value = 50.0;
 //值是否连续变化
 sl.continuous = NO;

//滑块的值
@property(nonatomic) float value;
//设置滑块的最小值(默认最小和最大分别是0.0和1.0)
@property(nonatomic) float minimumValue;
//设置滑块的最大值
@property(nonatomic) float maximumValue; 
//滑动时是否触发事件(默认是yes)
@property(nonatomic,getter=isContinuous) BOOL continuous;
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
//最后一个参数写UIControlEventValueChanged,监控滑动状态
//滑动时触发,如果continuous设为no的话就滑动结束时触发,打印slider.value


/**********************【UISegmentedControl】分段选取器************************/
    NSArray *titles = @[@"火影",@"海贼王"];
    UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:titles];
    seg.frame = CGRectMake(10,150,300,30);
    //设置选中的分段
    seg.selectedSegmentIndex = 0;
    //插入新的分段
    [seg insertSegmentWithTitle:@"银魂" atIndex:1 animated:YES];
//初始化传递进去的NSArray可以用字符串或图片
- (id)initWithItems:(NSArray *)items;
//设置frame
//设置风格样式(ios7中被禁用了)
@property(nonatomic) UISegmentedControlStyle segmentedControlStyle;
//设置哪个分段处于选中状态,不设置此属性,任何分段都处于非选中状态
@property(nonatomic) NSInteger selectedSegmentIndex;
//插入和删除某个分段

- (void)insertSegmentWithTitle:(NSString *)title atIndex:(NSUInteger)segment animated:(BOOL)animated;
- (void)removeSegmentAtIndex:(NSUInteger)segment animated:(BOOL)animated;
//删除所有分段,相当于把这个选取器给删了。。。
- (void)removeAllSegments;
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
//最后一个参数写UIControlEventValueChanged,监控点击状态
//点击时触发
//根据分段的下标,拿到分段的标题
- (NSString *)titleForSegmentAtIndex:(NSUInteger)segment;

/**********************【UISwitch】************************/
//【UISwitch】//开关控件,事件驱动型控件,固定大小51*31,frame中设置大小无效
//开关的打开状态
UISwitch * st = [[UISwitch alloc] initWithFrame:CGRectMake(10, 74, 300, 50)];
//on 控制开关开闭的属性
    st.on = YES;
  //UIControlEventValueChanged 控件值改变是,让id执行SEL
    [st addTarget:self action:@selector(stChanged:) forControlEvents:UIControlEventValueChanged];
@property(nonatomic,getter=isOn) BOOL on;
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
//最后一个参数写UIControlEventValueChanged
//点击开关时触发


/**********************【UIActivityIndicatorView】************************/
//加载等待提示控件,初始化的时候,设定风格样式,(联网小菊花)
    UIActivityIndicatorView * indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    //center设置视图中心点的坐标(CGPoint),一般直接设置中心点为view的中心点
    indicatorView.center = self.view.center;
    indicatorView.tag = 300;
    
    //startAnimating 开始转动
- (id)initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyle)style;
//UIActivityIndicatorViewStyleWhiteLarge,//控件的size固定

//开始旋转和停止
- (void)startAnimating;
- (void)stopAnimating;
//用switch控制activity 的旋转状态

/**********************【UIStepper】步进器************************/
//步进器 固定的size (94*27)修改无效, 事件驱动型控件
    UIStepper *st  = [[UIStepper alloc] initWithFrame:CGRectMake(10,74,300,30)];
    st.value = 10;
    //最值
    st.minimumValue = 10;
    st.maximumValue = 300;
    st.continuous = NO;
    //步长 + 一次增加的值
    st.stepValue  = 10;
    
//步进器的值
@property(nonatomic) double value;
//最小和最大值(默认0到100)
@property(nonatomic) double minimumValue;
@property(nonatomic) double maximumValue;
//设置步长 (默认值为1 必须>0)(加减的时候改变的值)
@property(nonatomic) double stepValue;

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
//最后一个参数写UIControlEventValueChanged
//点击加减的时候触发


/******************【UIProgressView】进度条,用于展示进度************************/
//进度条,高度固定为2,设置无效
    UIProgressView *progress = [[UIProgressView alloc] initWithFrame:CGRectMake(10,150,300,20)];
    //反映进度 progress (0.0-1.0)
    progress.progress =0.5;
//进度条的值 固定为0到1
@property(nonatomic) float progress;

//用stepper控制progress的进度

/******************【UIWebView】网页视图************************/

UIWebView *_webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
NSURL *url = [NSURL URLWithString:@"http://m.jd.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
[self.view addSubview:_webView];
[_webView release];


/******************【UIAlertView】警示框   重要************************/
    UIAlertView *alert  = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"流量用尽" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
    //展示警告框
    [alert show];

//代理
<UIAlertViewDelegate>

//代理方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"buttonIndex:%d",buttonIndex);
}




/******************【UIActionSheet】 事件表格************************/

//代理
<UIActionSheetDelegate>

 UIActionSheet *ac = [[UIActionSheet alloc] initWithTitle:@"分享" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"新浪微博",@"腾讯微博",nil];
    //从当前视图中,呈现出来
    [ac showInView:self.view];
[sheet showInView:self.view];
[sheet release];

//事件表代理方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"sheet index:%d",buttonIndex);
}
原文地址:https://www.cnblogs.com/liudongyan/p/4399334.html