UIButton和UITextField

本文详细介绍了iOS开发过程中的关键步骤与UI设计技巧,包括界面布局、控件使用、响应式设计以及最佳实践,旨在帮助开发者提高iOS应用的质量与用户体验。

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

UIButton和UITextField

import “AppDelegate.h”

@interface AppDelegate ()

@property(nonatomic,retain)UITextField *textField;
@property(nonatomic,assign)BOOL isSelect;
@property(nonatomic,retain)UILabel *label;

@end

@implementation AppDelegate

  • (void)dealloc
    {
    [_label release];
    [_textField release];
    [_window release];
    [super dealloc];
    }

  • (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    [_window release];

    // 新建一个button
    UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
    button.frame=CGRectMake(200, 300, 100, 50);
    button.backgroundColor=[UIColor redColor];
    [self.window addSubview:button];
    button.layer.borderWidth=1;
    button.layer.cornerRadius=20;
    [button setTitle:@”测试” forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.titleLabel.font=[UIFont systemFontOfSize:20];
    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

    // 创建一个textField
    self.textField=[[UITextField alloc] initWithFrame:CGRectMake(50, 100, 150, 50)];
    self.textField.backgroundColor=[UIColor yellowColor];
    [self.window addSubview:self.textField];
    [self.textField release];
    self.textField.layer.borderWidth=1;
    self.textField.layer.cornerRadius=20;
    self.textField.tag=1000;

    // 给textField添加addTarget-action方法
    [self.textField addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventEditingChanged];

    // 创建一个按钮,用来textField切换状态
    UIButton *button1=[UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame=CGRectMake(50, 160, 30, 30);
    [button1 setBackgroundImage:[UIImage imageNamed:@”check.png”] forState:UIControlStateNormal];
    [self.window addSubview:button1];

    [button1 addTarget:self action:@selector(check:) forControlEvents:UIControlEventTouchUpInside];
    self.isSelect=YES;

    // 给textField输入进去之后是圆点
    self.textField.secureTextEntry=YES;

    // label显示
    UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(90, 160, 150, 30)];
    label.backgroundColor=[UIColor whiteColor];
    [self.window addSubview:label];
    [label release];
    label.text=@”是否显示文本”;
    label.textColor=[UIColor redColor];
    label.textAlignment=NSTextAlignmentLeft;

    // 创建一个label
    self.label=[[UILabel alloc] initWithFrame:CGRectMake(210, 100, 150, 50)];
    self.label.backgroundColor=[UIColor whiteColor];
    [self.window addSubview:self.label];
    [self.label release];
    self.label.textColor=[UIColor greenColor];
    self.label.font=[UIFont systemFontOfSize:20];

    MyButton *muButton=[MyButton buttonWithType:UIButtonTypeSystem];
    muButton.frame=CGRectMake(50, 300, 100, 50);
    muButton.buttonName=@”张三”;
    [self.window addSubview:muButton];
    [muButton setTitle:@”MyButton” forState:UIControlStateNormal];
    muButton.titleLabel.font=[UIFont systemFontOfSize:20];

    return YES;
    }

-(void)click:(UIButton *)button{
// 先找到textField,然后再找到里面对应的内容
UITextField textField=(UITextField )[self.window viewWithTag:1000];
NSLog(@”%@”,textField.text);
NSLog(@”%@”,self.textField.text);
}

-(void)check:(UIButton *)button1{
if (self.isSelect) {
[button1 setBackgroundImage:[UIImage imageNamed:@”checked.png”] forState:UIControlStateNormal];
}else{
[button1 setBackgroundImage:[UIImage imageNamed:@”check.png”] forState:UIControlStateNormal];
}
self.isSelect=!self.isSelect;

self.textField.secureTextEntry=!self.textField.secureTextEntry;

}

-(void)changeValue:(UITextField *)textField{
NSLog(@”%@”,textField.text);
if (textField.text.length>5) {
self.label.text=@”密码长度符合”;
}else{
self.label.text=@”密码长度太短”;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值