#import <UIKit/UIKit.h>
@interface RootViewController :UIViewController
{
UITextField *text;
}
@end
//*********************************************************************//
#import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (void)viewDidLoad {
[superviewDidLoad];
//创建分段控件
NSArray * array = [NSArrayarrayWithObjects:@"亚洲",@"欧美",@"偷拍",@"日韩",nil];
UISegmentedControl *seg = [[UISegmentedControlalloc]initWithItems:array];
seg.frame =CGRectMake(10,50, self.view.frame.size.width - 20, 50);
[self.viewaddSubview:seg];
//默认选中某一项
seg.selectedSegmentIndex =1;
//----改变颜色
//seg.backgroundColor = [UIColor yellowColor];
seg.tintColor = [UIColorbrownColor];
//----添加图片
UIImage *image = [UIImageimageNamed:@"iconfont-yazhou"];
//防止图片被控件渲染
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[seg setImage:imageforSegmentAtIndex:3];
//----改变指定下标的文字
[seg setTitle:@"绿色"forSegmentAtIndex:0];
//给分段控件添加事件
[seg addTarget:selfaction:@selector(clickWhich:)forControlEvents:UIControlEventValueChanged];
//用开关(UISwitch)控制输入框是否允许输入
text = [[UITextFieldalloc]initWithFrame:CGRectMake(10,200, 280,40)];
text.placeholder =@"请输入";
text.backgroundColor = [UIColorgrayColor];
text.layer.cornerRadius =5;
[self.viewaddSubview:text];
//开关
UISwitch *mySwitch = [[UISwitchalloc]initWithFrame:CGRectMake(310,205, 0,0)];
[self.viewaddSubview:mySwitch];
//默认是开启状态
mySwitch.on =YES;
//给开关加一个事件
[mySwitch addTarget:selfaction:@selector(touchSwitch:)forControlEvents:UIControlEventValueChanged];
}
//分段控件点击执行的方法
- (void)clickWhich:(UISegmentedControl *)segment
{
NSInteger i = segment.selectedSegmentIndex;
NSLog(@"%ld",i);
}
- (void)touchSwitch:(UISwitch *)theSwitch
{
if (theSwitch.isOn ==YES) {
text.enabled =YES; //开关打开的时候允许输入
} else {
text.enabled =NO; //开关关闭的时候不允许输入
}
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end