UITextField的属性与程序启动后一系列方法

// UITextField 文本输入框

    // UITextField 继承与 UIControl

    // UIContr 继承与 UIView

   self.textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 200, 50)];

    self.textField.backgroundColor = KMyColor;

   // 设置代理

    self.textField.delegate = self;

    

   // 设置占位符

    self.textField.placeholder = @"请输入";

   

   // 字体大小

    self.textField.font = [UIFont systemFontOfSize:25];

   

    // 字体颜色

    self.textField.textColor = [UIColor redColor];

    

   // 对齐方式

   self.textField.textAlignment = NSTextAlignmentLeft;

    

   // 是否允许输入 也就是是否开启交互 默认是YES 支持交互

    // textField.enabled = YES;

    // 任何一个有交互行为的控件 交互默认都是打开的 但是我们也可以将其关闭

    //self.textField.userInteractionEnabled = YES;

    

    // 输入的时候 为密码模式(安全输入模式)

    //self.textField.secureTextEntry = YES;

    

    // 下一次输入的时候 是否清空上一次的内容

   //self.textField.clearsOnBeginEditing = YES;

    

    // 设置return键的样式 默认是换行

    //self.textField.returnKeyType = UIReturnKeySearch;

    

    // 设置键盘样式 默认的是英文

    //self.textField.keyboardType = UIKeyboardTypeNumberPad;

    

    // 是否显示 清除按钮 默认是不显示

    self.textField.clearButtonMode = UITextFieldViewModeAlways;

    

    // textField 添加左视图 和右视图

   

    // 自定义左边视图

    /*

    UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];

   leftView.backgroundColor = [UIColor greenColor];

    self.textField.leftView = leftView;

    self.textField.leftViewMode = UITextFieldViewModeAlways;

    [leftView release];

    // 自定义右边视图

    UIView *rightView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];

    rightView.backgroundColor = [UIColor redColor];

    self.textField.rightView = rightView;

    self.textField.rightViewMode = UITextFieldViewModeAlways;

    [rightView release];

     */

    // textField 设置边框圆角 常用的样式 因为比较好看

    self.textField.borderStyle = UITextBorderStyleRoundedRect;

    

    // 自定义键盘

   /*

    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];

    view.backgroundColor = [UIColor redColor];

    self.textField.inputView = view;

    [view release];

    */    

    // 自定义键盘上面的视图

    /*

    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];

    view.backgroundColor = [UIColor redColor];

   self.textField.inputAccessoryView = view;

    [view release];

    */

    

    // 等号左边的是set方法 要用self. 等号右边的是 get方法 要用_

    [self.window addSubview:_textField];

    [self.textField release];

    

    

// UIButton 按钮

    // UIButton 继承与 UIControl

    // UIControl 继承与 UIView

    // button 类方法创造出来 切记 不需要释放

    // button 添加背景图片 图片的宽高 需要和button的宽高结合起来使用 button的宽高 = 图片的宽高 / 2

    // 背景图片 button的类型必须是 UIButtonTypeSystem

    // 前景图片 button的类型必须是 UIButtonTypeCustom

    //UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    button.frame = CGRectMake(100, 200, 290 / 2 , 92 / 2);

    

   // button.backgroundColor = [UIColor redColor];

    // 设置button标题

    [button setTitle:@"按钮" forState:UIControlStateNormal];

    button.tag = 100;

 

    // 设置标题颜色

    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    // 取出button标题

    NSString *titleString = [button titleForState:UIControlStateNormal];

    NSLog(@"%@",titleString);

    [self.window addSubview:button];

    

    

    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];

    button2.frame = CGRectMake(100, 300, 290 / 2, 92 / 2);

    [button2 setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];

    [button2 setTitle:@"按钮2" forState:UIControlStateNormal];

    [self.window addSubview:button2];

    button2.tag = 200;

    

    

    // button 添加点击事件

    //[button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchDown];//点进去触发

    // 点下去松开才会触发

    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

 

    [button2 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

  

    

    // button设置背景图片

    // UIImage 继承与 NSObject 他不是一个控件

    // UIImage 需要通过 某一个控件 才能够展示出来

    /*

    [button setBackgroundImage:[UIImage imageNamed:@"denglu.png"] forState:UIControlStateNormal];

    // 取出button的背景图片

    UIImage *image = [button backgroundImageForState:UIControlStateNormal];

    [button setBackgroundImage:image forState:UIControlStateNormal];

     */

    

//    // button设置前景图片

//    [button setImage:[UIImage imageNamed:@"denglu.png"] forState:UIControlStateNormal];

//    [self.window addSubview:button];

//    

//    [button2 setImage:[UIImage imageNamed:@"zhuce.png"] forState:UIControlStateNormal];

//    [self.window addSubview:button2];

//    //button.clipsToBounds = YES;

//    //button.layer.masksToBounds = YES;

//    

//    // button取出前景图片

//    UIImage *image = [button imageForState:UIControlStateNormal];

//    [button setImage:image forState:UIControlStateNormal];

    

    return YES;

}

 

 

#pragma mark --- alertView代理方法------

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

    if (alertView.tag == 300)

    {

        if (buttonIndex == 0)

        {

            NSLog(@"点击了取消");

        }

        else if(buttonIndex == 1)

        {

            NSLog(@"点击了确定");

        }

        else if(buttonIndex == 2)

        {

            NSLog(@"点击了哈哈");

        }

    }

    else if (alertView.tag == 400)

    {

        if (buttonIndex == 0)

        {

            NSLog(@"点击了注册的取消");

        }

        else if(buttonIndex == 1)

        {

            NSLog(@"点击了注册的确定");

        }

        else if(buttonIndex == 2)

        {

            NSLog(@"点击了注册的哈哈");

        }

 

    }

}

 

// 这个代理方法 只是找到点击键盘的return

// 内部具体要实现什么样的效果 取决于代码

// 我们此时只是实现了 点击return 回收键盘

/*

- (void)buttonAction

{

    NSLog(@"正在录音");

    //self.window.backgroundColor = [UIColor redColor];

}

*/

- (void)buttonAction:(UIButton *)button

{

    if (button.tag == 100)

    {

        // 设置了代理之后 delegate后面要跟 self

        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"是否登录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",@"哈哈", nil];

        alertView.tag = 300;

        // 设置样式

        alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;

        // 取到提示框上的textField

        /*

        UITextField *textField = [alertView textFieldAtIndex:0];

        textField.backgroundColor = [UIColor orangeColor];

        */

        

        // 消息机制 几秒之后 self 去执行alertViewDismiss这个方法 传入的参数是alertView

        //[self performSelector:@selector(alertViewDismiss:) withObject:alertView afterDelay:3];

        

        // alertView展示出来

        // alertView层级 是最高的 不需要刻意添加

        [alertView show];

        [alertView release];

        //NSLog(@"button1");

    }

    else if (button.tag == 200)

    {

        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"是否登录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",@"哈哈", nil];

        alertView.tag = 400;

        [alertView show];

        [alertView release];

        // NSLog(@"button2");

    }

    //NSLog(@"发送消息");

    //self.window.backgroundColor = [UIColor greenColor];

 

}

#pragma mark --- alertView消失---

- (void)alertViewDismiss:(UIAlertView *)alertView

{

    [alertView dismissWithClickedButtonIndex:0 animated:YES];

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

    // 取消第一响应者

    [self.textField resignFirstResponder];

    return YES;

}

#pragma mark --- 点击空白处回收键盘 ---

//// 点击空白处 回收键盘

//- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

//{

//    //self.textField = self.window.subviews[0];

//    [self.textField resignFirstResponder];

//}

 

程序启动步骤

- (void)applicationWillResignActive:(UIApplication *)application

{

   // NSLog(@"将要进入到非活跃状态 就是将要进入到后台");

 

}

 

- (void)applicationDidEnterBackground:(UIApplication *)applicatio

{

   // NSLog(@"已经进入到非活跃状态 就是已经进入到后台");

 

}

 

- (void)applicationWillEnterForeground:(UIApplication *)application

{

   // NSLog(@"将要进入到活跃状态 就是将要进入到前台");

 

}

 

- (void)applicationDidBecomeActive:(UIApplication *)application

{

   // NSLog(@"已经进入到活跃状态 已经进入到前台");

 

}

 

- (void)applicationWillTerminate:(UIApplication *)application

{

   // NSLog(@"将要退出应用程序 就是滑飞");

 

}

 

posted @ 2016-02-23 20:41  mingxing  阅读(126)  评论(0编辑  收藏  举报