iOS分段控制器UISegmentedControl和开关UISwitch

#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


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值