01【iOS总结】UIView、UILabel、UITextField、UIButton 、目标动作机制(+UIAlertView、UIAlertController)

本文详细介绍UIKit中的核心控件,包括UIView、UILabel、UITextField、UIButton等的使用方法与属性设置,并探讨目标动作机制及UIAlertView、UIAlertController的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

01.UIView、UILabel、UITextField、UIButton 、目标动作机制(+UIAlertView、UIAlertController)

1.UIView

他是整个UI控件的父类,大部分控件的初始化方法都使用

    // 1.创建一个UIview对象(初始化)并设置它的frame(一个矩形)
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 150)];
    // 2.设置它的背景颜色
    myView.backgroundColor = [UIColor redColor];
    // 改变myView的bounds
    [myView setBounds:CGRectMake(50, 50, 100, 150)];
    // 改变myView的center
    [myView setCenter:CGPointMake(100, 100)];

    // 3.呈现view对象
    [self.window addSubview:myView];

    // 得到myView父view
    id supView = myView.superview;
    // 隐藏当前view
    myView.hidden = NO;
    // 设置透明度,取值范围(0,1),0纯透明,1不透明
    myView.alpha = 0.5;

    // 得到他的所有子类view 如果需要得到子视图中的某一类视图,需要遍历的时候配合iskindofclass来使用
//    myView.subviews;
for(UIView *subview in [cell.contentView subviews])//获取view的子视图       
//UITabViewCell中的contentView包括textlabel、imageView
[subview removeFromSuperview];//从父视图中移除子视图



    [self.window exchangeSubviewAtIndex:0 withSubviewAtIndex:4];// 交换两个指定索引位置的子视图
    [self.window exchangeSubviewAtIndex:1 withSubviewAtIndex:3];
    [self.window bringSubviewToFront: view5]; // 经某个view将该对象呈现在最外层

2.UIlable

需要掌握初始化方法,基本属性的使用。可以添加到父视图上。它主要侧重内容的显示。

UILabel* label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, realSize.width, realSize.height)];
UILabel* label=[[UILabel alloc] initWithFrame:CGRectZero];//不指定大小的初始化

label.numberOfLines=0;//设置最大行数,超过指定行则截断用省略号表示,默认为1,为0表示不限制
label.numberOfLines=3//限制行数有三行

label.lineBreakMode=UILineBreakModeCharacterWrap;//设置折行方式

label.text=str;
label.textColor=[UIColor blueColor];
lable.textAlignment=UITextAlignmentCenter;//设置字体对齐方式
label.backgroundColor=[UIColor clearColor]; //设置label背景色

label.font=font;
label.frame=CGRectMake(0,0,1,1);
self.contentLabel=label;

int val=[label.text intValue];//NSString到 intValue的转换


UILabel* label=(UILabel*)[self.view viewWithTag:100];
//每一个viewController都有一个self.view,viewWithTag:100找出该view上tag值为100的控件,返回值为UIView*类型

[self.view addSubview:self.contentLabel];

[label addSubview:img];

3.UITextField

需掌握初始化方法,基本属性的使用。要多熟悉它的代理方法。它主要侧重于用户的输入。

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, 250, 70)];
    textField.backgroundColor = [UIColor greenColor];
//    textField.text = @"请输入用户名:";
    // 给字体设置颜色
    textField.textColor = [UIColor blueColor];
    // 改变字体大小
    textField.font = [UIFont systemFontOfSize:12];
    // 得到系统所支持的所有字体
    NSLog(@"%@",[UIFont familyNames]);// 打印系统提供的字体
    // 设置字体
    textField.font = [UIFont fontWithName:@"Apple SD Gothic Neo" size:50];//
    // 设置占位字符(提示性文字)
    textField.placeholder = @"请输入邮箱:";
    // 设置键盘样式
//    textField.keyboardType = UIKeyboardTypeNamePhonePad;
    // 设置键盘外观
    textField.keyboardAppearance = UIKeyboardAppearanceDark;
    // 自动纠错
    textField.autocorrectionType = UITextAutocorrectionTypeYes;
    // 设置首字母大写(当首字母不能大写的时候,有可能和键盘样式有关)
    textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
    // 一键清除(右边的小叉号)
    textField.clearButtonMode = UITextFieldViewModeAlways;
    // 当变成编辑状态时,让该textField刚才所有的输入清除
    textField.clearsOnBeginEditing = YES;

    // 打开密码格式(密码样式可自定义)
    textField.secureTextEntry = YES;
    // 设置背景图片(如果不是png格式的,必须加后缀名,因为当没有后缀名的时候,系统默认是PNG格式的)
    textField.background = [UIImage imageNamed:@"picture.jpg"];
    // 设置边框样式(圆角扁平状)
    textField.borderStyle = UITextBorderStyleLine;
    // 文字的居左、居中、居右
    textField.textAlignment = NSTextAlignmentCenter;
    // 设置文字的垂直位置(Label没有该属性)
    textField.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
    // 设置文字的水平位置(Label没有该属性)
    textField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
  // 设置文字的水平位置(Label没有该属性)
    textField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

    // 不让进行编辑(交互)
    // enabled:直接改变控件本身的状态,例如:textField有可编辑状态和不可编辑状态。当设置为NO时,textField就会从可编辑状态改变到不可编辑状态。
    // userInteractionEnabled:不改变控件的状态,但是把用户交互关闭了,就是用户不可对该控件进行操作。
    textField.enabled = NO;
//    textField.userInteractionEnabled = NO;


    //自定义添加辅助视图(自定义)
    UIView *accessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, self.window.frame.size.height)];
    UIView *accessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.window.frame), 50)];
    [accessoryView setBackgroundColor: [UIColor yellowColor]];
    textField.inputAccessoryView = accessoryView;

    //自定义输入视图(默认是键盘)
    UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.window.frame), 256)];
    inputView.backgroundColor = [UIColor grayColor];
    textField.inputView = inputView;
    //左视图
    UILabel *leftLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, 100)];
    leftLable.backgroundColor = [UIColor purpleColor];
    textField.leftView = leftLable;
    textField.leftViewMode = UITextFieldViewModeAlways;
    //右视图
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage  imageNamed:@"picture.jpg"]];
    textField.rightView = imageView;
    textField.rightViewMode = UITextFieldViewModeAlways;

    [self.window addSubview:textField];

4.UIButton

按钮的初始化方法是使用的自己的,是一个便利构造器。这个和前两个有很大区别。要注意。按钮还有点击事件。很多时候点击无反应是我们将它的touchevent设置有误,最常用的是单击事件。

    //UIButton按钮自有的初始化方法
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];//便利构造器
    /*
    UIButtonTypeCustom = 0,
    UIButtonTypeDetailDisclosure,
    UIButtonTypeInfoLight,
    UIButtonTypeInfoDark,
    UIButtonTypeContactAdd,
    UIButtonTypeRoundedRect = UIButtonTypeSystem,
    */
    [btn setFrame:CGRectMake(200, 400, 100, 50)];
    //设置按钮标题
    // normal:是按钮常态,不做任何操作和设置情况下看到的按钮状态
    // highlighted:用户按住按钮不松手时候看到的状态
    // Disabled:关闭交互的状态的标题
    // Selected:按钮被选中状态
    // UIControlStateApplication:按钮作为应用启动图标
    // UIControlStateReserved:预留字段
    [btn setTitle:@"千万别摸我" forState:UIControlStateNormal];
    [btn setTitle:@"摸了就摸了" forState:UIControlStateHighlighted];
    // 关闭交互的状态的标题
//    [btn setTitle:@"摸我也没用" forState:UIControlStateDisabled];
//    [btn setEnabled:NO];
    // 按钮被选中状态
    [btn setTitle:@"一直在被摸" forState:UIControlStateSelected];
    // 设置按钮被选中
//    [btn setSelected:YES];

    // 改变标题颜色
    btn.tintColor = [UIColor orangeColor];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
    // 当点击时候有光圈效果
    btn.backgroundColor = [UIColor blueColor];
    btn.showsTouchWhenHighlighted = YES;

5.目标动作机制

  {  //给按钮添加点击事件(目标动作机制!)
    // Target:方法选择器的执行者(回调者)
    // action:按钮的回调方法(点击所触发的事件)如果回调方法带参数,只能是按钮本身
    // events:触发事件的点击模式
//    [btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];// btnAction是个方法名,需要实现该方法 // 不带参
    [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];// btnAction是个方法名,需要实现该方法,
    //关闭多点触控
    btn.multipleTouchEnabled = NO;
    [self.window addSubview:btn];
    textField.tag = 1000;
    return YES;
}
- (void)btnAction: (id)sender{
    UIButton *btn = (UIButton *)sender;
    [btn setTitle:@"你是被莫大的" forState:UIControlStateNormal];
    // 随机更改window颜色
    float a = arc4random()%256;
    self.window.backgroundColor = [UIColor colorWithRed:a/255.0 green:a/255.0 blue:a/255.0 alpha:1.0];
    //点击按钮可以得到文本输入框中的文字
    //textField不能是全局变量或者属性
    // 给field添加tag值,tag值的作用是,为当前view或者其子类添加标记,当前view或者其子类的父视图可以通过tag找到该视图。

    // 父视图通过tag值得到field
    UITextField *resultTextField = (UITextField *)[self.window viewWithTag:1000];
    // 得到文本输入框中的文字
    NSString *fieldTextStr = resultTextField.text;
    NSLog(@"%@", fieldTextStr);


    NSLog(@"你点");
}

6.UIAlertView

//添加警示框
- (void)viewDidLoad {
    [super viewDidLoad];
    //1.创建button
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    //设置按钮的frame和title
    [btn setFrame:CGRectMake(100, 100, 100, 100)];
    [btn setTitle:@"警示框" forState:UIControlStateNormal];
    //设置点击事件
    [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    //显示按钮
    [self.view addSubview:btn];
}

//4.实现btn点击事件
- (void)btnAction: (UIButton *)sender{
    // 创建一个UIAlertView对话框
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"我是提示标题" message:@"我是内容" delegate:self cancelButtonTitle:@"我是取消" otherButtonTitles:@"我是确定1", @"我是确定2", nil];
    //显示alterView
    [alertView show];
}

7.UIAlertController

//添加警示框
- (void)viewDidLoad {
    [super viewDidLoad];
    //新型弹出框按钮
    UIButton *newBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [newBtn setTitle:@"新警示框" forState:UIControlStateNormal];
    [newBtn setFrame:CGRectMake(300, 200, 100, 100)];
    [newBtn addTarget:self action:@selector(newBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:newBtn];
}

//新弹出框按钮事件
- (void)newBtnAction: (UIButton *)sender{
    // 创建一个警示框控制器  
    // 这是iOS8之后出的新类,用来替换原来的UIAlertView 和 UIActionSheet (不需要代理)
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"新标题" message:@"发红包" preferredStyle: UIAlertControllerStyleAlert];

    //为警示框添加点击按钮(内部实现)
    //title:警示框按钮标题
    //style:按钮的样式
    //hander:点击按钮就会执行的语句(block)
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"我点击了取消按钮");
    }];

    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        NSLog(@"我点击了确定按钮");
    }];
    //将创建好的action添加到警示框视图控制器上
    [alertC addAction:cancelAction];
    [alertC addAction:sureAction];

    //视图控制器之间切换的一种样式,称之为【模态】
    //Controller:将要模态出来的视图控制器
    //animated:模态推出视图控制器的过程是否需要动画
    //completion:模态推出视图控制器之后将要执行的操作
    [self presentViewController:alertC animated:YES completion:^{
        NSLog(@"我是新警示框的视图控制器,我将要显示....");
    }];

}
注意:

1.bounds 是针对自己的坐标系系统
改变自身 bounds 的值不会影响到自身的 x,y ,只会影响到子视图的坐标系!
如果 bounds 的值改为负数,它自身的原点会向右下偏移
如果 bounds 的值改为正数,它自身的原点会向左上偏移

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值