UI第三节复习

UIButton

创建UIButton与创建UILabel UITextField UIView 的步骤很相似

1.创建button 对象 (如果本类有初始化方法, 使用自己的; 否则使用父类的)

2.设置按钮显示相关的属性

3.为按钮添加点击事件

4.添加按钮到父视图上, 用以显示

5.按钮无需释放(因为使用的是类方法创建的button)

UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
loginButton.frame = CGRectMake(30, 200, 60, 30);
[loginButton setTitle:@"登录" forState:UIControlStateNormal];
[loginButton addTarget:self action:@selector(login:)
forControlEvents:UIControlEventTouchUpInside];
[containerView addSubview:loginButton];


UIButton添加事件
[loginButton addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpIn side];// 为按钮添加事件, 指定按钮点击之后, 执行target的action方法
[loginButton removeTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpIn side];// 移除按钮的点击事件


外观控制

[loginButton setImage:[UIImage imageNamed:@"login.png"] forState:UIControlStateNormal];// 设置指定状态下的前景图片
UIImage *normalImage = [loginButton [loginButton setTitleColor:[UIColor imageForState:UIControlStateNormal];// 获取指定状态下的前景图片
[loginButton setBackgroundImage:[UIImage imageNamed:@“login2.png”] forState:UIControlStateNormal];// 设置指定状态下的背景图片
UIImage *normalBackgroundImage = [loginButton backgroundImageForState:UIControlStateNormal];// 获取指定状态下的背景图片


UIAlertView

创建UIAlertView的步骤如下

1.开辟空间并初始化视图(初始化时, 给出视图位置和大小)

2.调用UIAlertView的show方法, 呈现提示框

3.释放UIAlertView对象

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提⽰示" message:@"请妥善保管好你的密码" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alertView show];
[alertView release];

在确定后面可以按需求在添加按钮


alertView响应点击

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    // 默认的取消按钮 buttonIndex为0
    // 其他的按钮依次为1 2 3 ....
    NSLog(@"%d", buttonIndex);
    if (1 == buttonIndex) {
        NSLog(@"请重新输入用户名");
    } else {
        NSLog(@"请重新");
    }
}

学到这些就可以写一个 非常简单的登录界面了

- (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];
    
    
    UIView *viewB = [[UIView alloc] initWithFrame:CGRectMake(20, 100, 280, 280)];
    [viewB setBackgroundColor:[UIColor lightGrayColor]];
    viewB.layer.cornerRadius = 10;
    viewB.alpha = 0.2;
    [self.window addSubview:viewB];
    
    UIView *viewT = [[UIView alloc] initWithFrame:CGRectMake(20, 100, 280, 25)];
    [viewT setBackgroundColor:[UIColor lightGrayColor]];
    viewT.layer.cornerRadius = 10;
    [self.window addSubview:viewT];

    UIView *viewT1 = [[UIView alloc] initWithFrame:CGRectMake(20, 110, 280, 25)];
    [viewT1 setBackgroundColor:[UIColor lightGrayColor]];
    [self.window addSubview:viewT1];
    
    UILabel *userName = [[UILabel alloc] initWithFrame:CGRectMake(35, 170, 100, 50)];
    
    [userName setText:@"用户名:"];
    [userName setTextColor:[UIColor blackColor]];
    [userName setBackgroundColor:[UIColor clearColor]];
    
    [self.window addSubview:userName];
    
    UILabel *password = [[UILabel alloc] initWithFrame:CGRectMake(35, 210, 100, 50)];
    [password setText:@"密码:"];
    [password setTextColor:[UIColor blackColor]];
    [password setBackgroundColor:[UIColor clearColor]];
    
    [self.window addSubview:password];
    
    UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(100, 180, 185, 30)];
    [textField1 setTextColor:[UIColor blackColor]];
    [textField1 setBackgroundColor:[UIColor whiteColor]];
    [textField1 setPlaceholder:@"请输入手机/邮箱/QQ"];
    [textField1 setBorderStyle:UITextBorderStyleRoundedRect];
    // 给textfield设置tag值 方便父视图查找
    // tag值一般不建议设置为0 因为0是所有视图对象的默认值
    textField1.tag = 10000;
    
    [self.window addSubview:textField1];
    
    UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(100, 220, 185, 30)];
    [textField2 setTextColor:[UIColor blackColor]];
    [textField2 setBackgroundColor:[UIColor whiteColor]];
    [textField2 setSecureTextEntry:YES];
    [textField2 setKeyboardType:UIKeyboardTypeNumberPad];
    [textField2 setBorderStyle:UITextBorderStyleRoundedRect];
    
    [self.window addSubview:textField2];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    [button1 setTitle:@"登  录" forState:UIControlStateNormal];
    [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button1 setFrame:CGRectMake(60, 280, 80, 30)];
    [button1 setBackgroundColor:[UIColor whiteColor]];
    button1.alpha = 0.5;
    button1.layer.cornerRadius = 10;
    [button1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:button1];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
    [button2 setTitle:@"注  册" forState:UIControlStateNormal];
    [button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button2 setFrame:CGRectMake(180, 280, 80, 30)];
    [button2 setBackgroundColor:[UIColor whiteColor]];
    button2.alpha = 0.5;
    button2.layer.cornerRadius = 10;
    
    [button2 addTarget:self action:@selector(buttonClicked1:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.window addSubview:button2];
    
    [viewB release];
    [viewT release];
    [viewT1 release];
    [userName release];
    [password release];
    [textField1 release];
    [textField2 release];
    [_window release];

    return YES;
}


- (void)buttonClicked:(UIButton *)button
{
    // 获取用户名的textField
    // 1.通过父视图的subviews属性 找子视图
    // 2.通过tag值搜索 某一个子视图
    UITextField *textfield = (UITextField *)[self.window viewWithTag:10000];
    NSLog(@"输入的用户名为 : %@", textfield.text);
    
    if ([textfield.text isEqualToString:@"ddd"]) {
        NSLog(@"登录成功");
    } else {
        // 提示窗口
        // 1. 创建
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请确认用户名" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        // 2. 显示
        [alertView show];
        // 3. 内存管理
        [alertView release];
        NSLog(@"登录失败");
    }
    
    // 让textfield弹出的键盘回收
    [textfield resignFirstResponder]; 
}


static int count = 0;
- (void)buttonClicked1:(UIButton *)button
{
    
    if (count >= 3) {
        NSLog(@"别特么点了---注册不了!");
    } else {
        NSLog(@"亲---------注册不了");
    }
    count ++;
}

- (void)dealloc
{
    // 属性的内存管理
    [_window release];
    [super dealloc];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    // 默认的取消按钮 buttonIndex为0
    // 其他的按钮依次为1 2 3 ....
    NSLog(@"%d", buttonIndex);
    if (1 == buttonIndex) {
        NSLog(@"请重新输入用户名");
    } else {
        NSLog(@"请重新输入用户名");
    }
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值