九种常用控件

目录
  1.1 UISegmentedControl
  1.2 UISwitch
  1.3 UIActivityIndicatorView
  1.4 UISlider
  1.5 UIStepper
  1.6 UIProgressView
  1.7 UITextView
  1.8 UIActionSheet
  1.9 UIAlertView
1.1 UISegmentedControl

NSArray *titleArray=@[@"最新活动",@"我的活动"];

UISegmentedControl *segment =[[UISegmentedControl alloc]initWithItems:titleArray]; segment.frame=CGRectMake(48, 100, 220, 30);

//背景颜色
segment.tintColor=[UIColor orangeColor];

//设置字体大小
[segment setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15],NSFontAttributeName, nil] forState:UIControlStateNormal];

//添加点击事件
[segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];

//设置默认选择项
segment.selectedSegmentIndex=0;

//点击后是否恢复原样
segment.momentary = YES;

//设置某一项宽度
[segmented setWidth:70.0 forSegmentAtIndex:2]; [self.view addSubview:segment];


方法实现

- (void)segmentAction:(UISegmentedControl *)seg{
    
    if (seg.selectedSegmentIndex==0) {
        
        NSLog(@"^o^");
        
    }
    else{
        
        NSLog(@"oo");
        
    }
    
}

1.2 UISwitch

//细节: 开关控件大小是固定的 (大小: 51X31)

UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(100, 100, 0, 0)];

mySwitch.onTintColor = [UIColor redColor];

mySwitch.tintColor = [UIColor blueColor];

mySwitch.thumbTintColor = [UIColor yellowColor];

[self.view addSubview:mySwitch];

//不要添加touchUpInside事件

[mySwitch addTarget:self action:@selector(dealSwitch:) forControlEvents:UIControlEventValueChanged];


方法实现

-(void)dealSwitch:(UISwitch *)s
{
    NSLog(@"%d",s.isOn);
    
}

1.3 UIActivityIndicatorView

UIActivityIndicatorView *aiv = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

aiv.center = CGPointMake(160, 240);

[self.view addSubview:aiv];

[aiv startAnimating];

//模拟下载完成
[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(downloadFinish:) userInfo:aiv repeats:NO];


方法实现

-(void)downloadFinish:(NSTimer *)timer
{
    
    UIActivityIndicatorView *aiv = timer.userInfo;
    [aiv stopAnimating];
    
}

1.4 UISlider

UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];

slider.minimumValue = 0;

slider.maximumValue = 100;

slider.value = 50;

[self.view addSubview:slider];

//事件处理

[slider addTarget:self action:@selector(dealSlider:)
 
 forControlEvents:UIControlEventValueChanged];


方法实现

-(void)dealSlider:(UISlider *)slider
{
    
    NSLog(@"slider value = %f",slider.value);
}
1.5 UIStepper

//演示stepper和文本输入框的联合使用

// 控制购买商品的数据

UITextField *goodsNumTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, 70, 30)];

goodsNumTextFiled.borderStyle = UITextBorderStyleRoundedRect;

goodsNumTextFiled.delegate = self;

goodsNumTextFiled.tag = 100;

goodsNumTextFiled.text = @"1";

[self.view addSubview:goodsNumTextFiled];

UIStepper *stepper = [[UIStepper alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];

stepper.minimumValue = 1;

stepper.maximumValue = 999999999999;

stepper.value = 2;

stepper.stepValue = 1; //每次+1

[self.view addSubview:stepper];

//添加事件处理

[stepper addTarget:self action:@selector(dealStepper:) forControlEvents:UIControlEventValueChanged];

方法实现


-(void)dealStepper:(UIStepper *)stepper
{
    //取得textFiled指针
    UITextField *tf = (UITextField *)[self.view viewWithTag:100];
    tf.text = [NSString stringWithFormat:@"%d",(int)stepper.value];
}

1.6 UIProgressView

//创建的同时指定风格

UIProgressView *progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];

progressView.frame = CGRectMake(100, 100, 200, 30);

//值的范围是0.0~1.0

progressView.progress = 0.5;

[self.view addSubview:progressView];

//模拟数据下载的过程

[NSTimer scheduledTimerWithTimeInterval:1/60.0 target:self selector:@selector(dealProgress:) userInfo:progressView repeats:YES];

方法实现


-(void)dealProgress:(NSTimer *)timer
{
    
    timer.userInfo;
    pv.progress += 0.004;
    if(pv.progress >=1)
    {
        pv.progress=0;
    }
    
}
1.7 UITextView

UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 30, 300, 450)];

textView.text = @"尽人事,听天命? 非也, 我命由我不由天。";

textView.backgroundColor = [UIColor orangeColor];

textView.font = [UIFont systemFontOfSize:18];

textView.tag = 100;

[self.view addSubview:textView];

1.8 UIActionSheet

UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:@"分享" delegate:self cancelButtonTitle:@"取消按钮" destructiveButtonTitle:nil otherButtonTitles:@"新浪分享",@"微信分享",@"QQ分享",@"短信",@"邮件分享",nil];

[action showInView:self.view];

方法实现


- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    NSLog(@"buttonIndex = %ld",(long)buttonIndex);
    
}

1.9 UIAlertView

1.创建带有输入框的警告视图(带有一个输入框)

UIAlertView *alertView = [[UIAlertView alloc] init];

alertView.alertViewStyle = UIAlertViewStylePlainTextInput;

alertView.title = @"警告";

alertView.message = @"我是警告视图";

[alertView addButtonWithTitle:@"退出"];

[alertView show];

//处理警告视图的事件

//需要遵守协议 UIAlertViewDelegate

alertView.delegate = self;


方法实现

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    //获取警告视图中得 输入框的文本, 如何获取?
    
    UITextField *nameTextFiled = [alertView textFieldAtIndex:0];
    
    NSLog(@"nameTextFiled.text = %@",nameTextFiled.text);
    
}

你可能感兴趣的:(九种常用控件)