UIButton

本文介绍了一个简单的iOS应用程序启动过程及主界面的搭建方法,包括使用Objective-C创建AppDelegate类来处理应用程序的启动、配置UI窗口背景颜色并使其可见、添加按钮及文本输入框等控件,并实现按钮点击事件及文本框输入返回键的功能。

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

AppDelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end

AppDelegate.m

#import "AppDelegate.h"

@interface AppDelegate ()<UITextFieldDelegate>

@property(nonatomic, assign)BOOL isSelected;
@property(nonatomic, retain)UITextField *textField;

@end

@implementation AppDelegate

- (void)dealloc {
    [_window release];
    [_textField 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(100, 100, 150, 50);
    button.backgroundColor = [UIColor yellowColor];
    [self.window addSubview:button];
    //  Button用便利构造器创建, 不需要释放

    //  添加边框, 添加弧度
    button.layer.borderWidth = 1;
    button.layer.cornerRadius = 10;

    //  添加一个标题
    [button setTitle:@"确认" forState:UIControlStateNormal];
    //  修改字体大小
    button.titleLabel.font = [UIFont systemFontOfSize:25];
    //  修改颜色
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    //  绑定一个点击方法
    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

    //  创建一个textField
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 150, 50)];
    self.textField.layer.borderWidth = 1;
    self.textField.layer.cornerRadius = 10;
    [self.window addSubview:self.textField];
    [_textField release];
    self.textField.secureTextEntry = YES;

    //  设置代理人
    self.textField.delegate = self;





    return YES;
}

//  点击return, 回收键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    NSLog(@"1111111");
    //  失去第一响应者
    [textField resignFirstResponder];
    return YES;
}




- (void)click:(UIButton *)button {
    if ([button.currentTitle isEqualToString:@"确认"]) {
        NSLog(@"%@", self.textField.text);
        [button setTitle:@"取消" forState:UIControlStateNormal];
    } else {
        [button setTitle:@"确认" forState:UIControlStateNormal];
    }

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

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值