#Objective - C - UI-design - 第二天 - UIKit框架-UIKit-基本控件-相关习题

本文介绍了iOS开发中UI四大常用控件UILabel、UITextField、UIButton和UIImageView的使用,包括属性设置、交互功能及实战小习题,如按钮换图、单选、多选和文本传递。
摘要由CSDN通过智能技术生成

UI四大常用控件:UILabel, UITextField, UIButton, UIImageView及相关小习题


UILabel

  • UILabel(标签): 是显示文本的控件
  • UILabel是UIView子类,UILabel是能显⽰示文字的视图。
    UILabel *userNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 100, 100, 30)];
    userNameLabel.text = @“用户名”; 
    [containerView addSubview:userNameLabel]; 
    [userNameLabel release];
注:containerView 已经添加到根视图上

UILabel属性设置:
IULabel


UITextField

  • UITextField(输入框):是控制文本输入和显示的控件。
  • UITextField允许用户编辑文字(输入)
// 使⽤用初始化⽅方法创建对象
    UITextField *userNameTextField = [[UITextField
 alloc] initWithFrame:CGRectMake(100, 100, 190, 30)];
// 设置边框⻛风格
    userNameTextField.borderStyle =
 UITextBorderStyleRoundedRect;
// 设置占位符
    userNameTextField.placeholder = @“⼿手机号/邮箱";
    [containerView addSubview:userNameTextField];
    [userNameTextField release];

占位符:
placeholder:占位字符串(没有任何输入时,给出的提示字符串)

    textField.placeholder = @“请输⼊入⽤用户名”;

输入控制属性:
UITextFiedl

外观属性:
这里写图片描述


TextField常用代理方法:

    //当textField将要开始编辑的时候告诉委托人
 - (BOOL)textFieldShouldBeginEditing:(UITextField
 *)textField
    //当textField已经编辑的时候告诉委托人
 - (void)textFieldDidBeginEditing:(UITextField
 *)textField
    //当textField将要完成编辑的时候告诉委托人
 - (BOOL)textFieldShouldEndEditing:(UITextField
 *)textField
    //当textField已经完成编辑的时候告诉委托人
 - (void)textFieldDidEndEditing:(UITextField *)textField 
    //当点击键盘上回车按键时候告诉委托人
 - (BOOL)textFieldShouldReturn:(UITextField *)textField 
    //- 注: 委托⼈人即delegate

UIButton

  • UIButton(按钮):是响应用户点击的控件。

UIbutton初始化方法相对不同

// 便利构造器方法创建对象
    UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
    loginButton.frame = CGRectMake(30, 200, 60, 30);
// 设置button的标题 
    [loginButton setTitle:@"登录" forState:UIControlStateNormal];
// 添加点击事件
    [loginButton addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
    [containerView addSubview:loginButton];

UIButton外观

这里写图片描述

外观二
这里写图片描述


UIImageView

  • UIImageView是IOS里用来显示图片的类

创建image:

// 图片文件路径
    NSString *path = [[NSBundle mainBundle]
pathForResource:@"1" ofType:@"jpg"];
// 创建一个UIImage对象,使用initWithContentOfFile: 方法
    UIImage *image = [UIImage
imageWithContentsOfFile:path];
// 创建一个UIImageView对象,使用initWithImage: 方法
    UIImageView *imageView = [[UIImageView alloc]
initWithImage:image];
    imageView.frame = CGRec
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值