[2]基础视图、程序启动流程(UITextField,UIButton,deleagte,程序启动流程)

UITextField

    UITextField *passwordFied = [[UITextField alloc] initWithFrame:CGRectMake(160, 180, 160, 40)];
    passwordFied.borderStyle = UITextBorderStyleRoundedRect;
    //        UITextBorderStyleNone, 无边框
    //        UITextBorderStyleLine, 线
    //        UITextBorderStyleBezel, 阴影框
    //        UITextBorderStyleRoundedRect 圆角
    passwordFied.secureTextEntry = YES;//让输入的内容以黑点的形式显现,实现密文功能.
    passwordFied.keyboardType = UIKeyboardTypeNamePhonePad;
//    UIKeyboardTypeDefault,                // Default type for the current input method.
//    UIKeyboardTypeASCIICapable,           // Displays a keyboard which can enter ASCII characters, non-ASCII keyboards remain active
//    UIKeyboardTypeNumbersAndPunctuation,  // Numbers and assorted punctuation.
//    UIKeyboardTypeURL,                    // A type optimized for URL entry (shows . / .com prominently).
//    UIKeyboardTypeNumberPad,              // A number pad (0-9). Suitable for PIN entry.
//    UIKeyboardTypePhonePad,               // A phone pad (1-9, *, 0, #, with letters under the numbers).
//    UIKeyboardTypeNamePhonePad,           // A type optimized for entering a person's name or phone number.
//    UIKeyboardTypeEmailAddress,           // A type optimized for multiple email address entry (shows space @ . prominently).
//    UIKeyboardTypeDecimalPad NS_ENUM_AVAILABLE_IOS(4_1),   // A number pad with a decimal point.
//    UIKeyboardTypeTwitter NS_ENUM_AVAILABLE_IOS(5_0),      // A type optimized for twitter text entry (easy access to @ #)
//    UIKeyboardTypeWebSearch NS_ENUM_AVAILABLE_IOS(7_0),    // A default keyboard type with URL-oriented addition (shows space . prominently).
//    
//    UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // Deprecated
    passwordFied.tag = 1002;
    //    passwordFied.delegate = self;
    passwordFied.placeholder = @"请输入密码";//占位符,提示用户这个输入框输入什么内容
   
    passwordFied.autocapitalizationType = UITextAutocapitalizationTypeNone;//这个属性修改首字母默认大写,当你输入一个邮箱的时候,程序会认为你输入的英文.
    [passwordFied becomeFirstResponder];//通过这句代码,当程序一启动时,就会弹出键盘,让用户这输入框,成为第一响应者.
    [containerView addSubview:passwordFied];//给containerView视图添加 passwordFied输入框

UIBotton

    UIButton *registButton = [UIButton buttonWithType:UIButtonTypeSystem];//创建UIBotton对象
    registButton.frame = CGRectMake(30, 240, 90, 40);
    
    [registButton setTitle:@"注册" forState:UIControlStateNormal];//给button添加一个标题显示
    registButton.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    
    [registButton setBackgroundImage:[UIImage imageNamed:@"100.jpg"] forState:UIControlStateNormal];//设置背景图片
    
//    registButton.titleLabel.textColor = [UIColor redColor];
    [registButton addTarget:self action:@selector(enterRegist) forControlEvents:UIControlEventTouchUpInside ]; //给Button添加事件,enterRegist是我们自己写的一个事件想应方法
    /*
     - (void)enterLogin
    {
         NSLog(@"进入登录页面");
    }   
    */
    [containerView addSubview:registButton];
    registButton.backgroundColor = [UIColor grayColor];
    [registButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];//修改字体颜色

6种回收键盘的方法

1.tag 值找到回收键盘的输入框

UITextField *tf = (UITextField *)[self.window viewWithTag:1001];
[tf resignFirstResponder];

2.这种方法比较低效,因为需要把所有的子视图全部遍历一遍,将能编辑的空间结束编辑,也就是收回键盘.

[self.view endEding:YES];
[sefl.window.subview[0] endEdinting:YES];

3通过找子视图集合中的某个视图对象,使其放弃第一种响应者

//此处把view做了视图
@property (retain,nonatomic) UIView *view;
[self.window.subviews[0] subviews]; //获containerView的所有子视图
[[self.window.subviews[0] subview][2] endEditing:YES];//与下面的效果一样
[[self.window.subviews[0] subviews][2] resignFirstResponder];//释放响应

4属性

.h文件中 声明下属性tf
@property (retain,nonatomic)  UITextField *tf;
[self.tf resignFirstResponder];

5 按钮事件

//此种方法是在按钮事件上做了手脚 如果事件这么写 就会点击登录后关闭键盘
[loginButton addTarget:userNameFieldaction:@selector(resignFirstResponder) forControlEvents:UIControlEventTouchUpInside];

6代理delagate

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
   [textField resignFirstResponder];  
}
userNameField.delegate = self;  //对应的输入框要加句代码

如果有俩个输入框,第一个输入框输完next到下一个,下一个输入结束才隐藏键盘的方法

点return回收键盘

.h文件
@interface AppDelegate : UIResponder <UIApplicationDelegate,UITextFieldDelegate> // 添加协议UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{    
    //账户 和密码
    UITextField *tf = (UITextField *)[self.window viewWithTag:1002];
    if(textField.tag == 1001)
    {
        [tf becomeFirstResponder];
    }
    else if(textField.tag == 1002)
    {
        [textField resignFirstResponder];
      
    }
    return  YES;

}
userNameField.tag = 1001;
passwordFied.tag = 1002;
userNameField.delegate = self;
passwordFied.delegate = self;

程序启动流

我们给每个方法都写了一段代码
NSLog(@"%s ,%d",__FUNCTION__,__LINE__); //输出方法名 及代码第多少行
运行后输出为
2015-04-21 20:48:44.786 Exercise[15585:753365] 应用程序加载完毕 ,启动的方法-[AppDelegate application:didFinishLaunchingWithOptions:] ,83
2015-04-21 20:48:45.266 Exercise[15585:753365] Application windows are expected to have a root view controller at the end of application launch
2015-04-21 20:48:45.352 Exercise[15585:753365] 进入前台,进入活跃状态,刷新页面-[AppDelegate applicationDidBecomeActive:] ,253
2015-04-21 20:48:49.158 Exercise[15585:753365] 应用程序即将取消活跃状态-[AppDelegate applicationWillResignActive:] ,234
2015-04-21 20:48:49.712 Exercise[15585:753365] 应用程序进入后台-[AppDelegate applicationDidEnterBackground:] ,241
2015-04-21 20:48:50.786 Exercise[15585:753365] 将要进入前台-[AppDelegate applicationWillEnterForeground:] ,247
2015-04-21 20:48:51.326 Exercise[15585:753365] 进入前台,进入活跃状态,刷新页面-[AppDelegate applicationDidBecomeActive:] ,253

UIAlertView 小知识

//创建方式
UIAlertView *a1 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"欢迎回来" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];

//判断输入框的账号密码是否正确 并返回 
- (void)enterDelu
{
    UITextField *tf1 = (UITextField *)[self.window viewWithTag:1001];
    UITextField *tf2 = (UITextField *)[self.window viewWithTag:1002];
    UIAlertView *a1 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"欢迎回来" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    UIAlertView *a2 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"密码错误" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    NSLog(@"%@",tf1);
    
    if ([tf1.text isEqual: @"anhao"] && [tf2.text isEqual: @"anhao"])
    {
        [a1 show];
    }
    else
    {
        [a2 show];
    }

}

小技巧

怎么改变uitextfield placeholder的颜色和位置?

//继承uitextfield,重写这个方法
- (void) drawPlaceholderInRect:(CGRect)rect {
    [[UIColor blueColor] setFill];
    [self.placeholder drawInRect:rect withFont:self.font lineBreakMode:UILineBreakModeTailTruncation alignment:self.textAlignment];
}

uitextfield可参考 http://blog.csdn.net/ysy441088327/article/details/7625000

On the road。。。
原文地址:https://www.cnblogs.com/ianhao/p/4445453.html