码迷,mamicode.com
首页 > 其他好文 > 详细

UI_02 基础视图、程序启动流程

时间:2015-08-17 21:59:32      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

?、UITextField

UITextField(输?框):是控制?本输?和显?的控件

UITextField核?功能主要包含3个??: ?本显? 输?控制 外观配置

1、?本显?

//    textField.text = @"你好";

//    textField.textAlignment = NSTextAlignmentCenter;

    textField.textColor = [UIColor blueColor];

    textField.placeholder = @"请输入我爱编程";

    textField.font = [UIFont fontWithName:@"" size:20];


2、输?控制

    textField.enabled = YES;
    textField.
clearsOnBeginEditing = NO;
    textField.
secureTextEntry = YES;
    textField.
keyboardType = UIKeyboardTypeDefault;

    textField.returnKeyType = UIReturnKeyDefault;

//自定义键盘视图
   
UIView * inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 180)];
    inputView.
backgroundColor = [UIColor blueColor];
    textField.
inputView = inputView;
    [inputView
release];
//自定义键盘辅助视图
   
UIView * inputAccessaryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 60)];
    inputAccessaryView.
backgroundColor = [UIColor yellowColor];
    textField.
inputAccessoryView = inputAccessaryView;

    [inputAccessaryView release];


3、外观控制

    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.
clearButtonMode = UITextFieldViewModeWhileEditing;

    //设置文本框左视图

    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];

    button.frame = CGRectMake(0, 0, 40, 40);

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

    textField.leftView = button;

    textField.leftViewMode = UITextFieldViewModeAlways;



二、UIButton

    UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
//    button.backgroundColor = [UIColor blackColor];
    button.
frame = CGRectMake(30, 100, 50, 50);
//    button.layer.cornerRadius = 20;
   
//设置前景图片
    [button
setImage:[UIImage imageNamed:@"" ] forState:UIControlStateNormal];
   
//设置背景图片
   
//设置普通状态下的背景图片
    [button
setBackgroundImage:[UIImage imageNamed:@"normal.png"] forState:UIControlStateNormal];
   
//设置高亮状态下的背景图片
    [button
setBackgroundImage:[UIImage imageNamed:@"hilighted.png"] forState:UIControlStateHighlighted];
   
//添加事件
    [button
addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
   
//设置按钮title
    [button
setTitle:@"绿鸟" forState:UIControlStateNormal];
    [button
setTitle:@"蓝鸟" forState:UIControlStateHighlighted];

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


- (void)clickButton:(UIButton *)button
{
   
self.window.backgroundColor = [UIColor colorWithRed:random()%100/100.0 green:random()%100/100.0 blue:random()%100/100.0 alpha:1];
    button.
backgroundColor = [UIColor whiteColor];

}



三、AppDelegate

****键盘收回****

1、将AppDelegate作为UITextField的delegate

@interface AppDelegate : UIResponder <UIApplicationDelegate, UITextFieldDelegate>

2、AppDelegate.m?件实现textFieldShouldReturn:?法

- (void)textFieldDidBeginEditing:(UITextField *)textField

{
    [textField
becomeFirstResponder];
   
NSLog(@"%s", __FUNCTION__);

}// became first responder

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{

    NSLog(@"%s", __FUNCTION__);     // 输出方法名

    [textField resignFirstResponder];
   
return YES;

}// called when ‘return‘ key pressed. return NO to ignore.

3、添加代理

    textField.delegate = self;


****触摸键盘外空白区域隐藏键盘****

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

{
   
UITouch *touch = [touches anyObject];
   
   
if (![touch.view isKindOfClass: [UITextField class]] || ![touch.view isKindOfClass: [UITextView class]]) {
       

        [self.window endEditing:YES];       

    }

    [super touchesBegan:touches withEvent:event];

}



四、iOS程序启动流程

1、程序入口

int main(int argc, char * argv[]) {

    @autoreleasepool {

        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }

}


UIApplicationMain剖析:

    argc: argv[].count

    argv[]: 存放的是所有的参数

    principalClassName:如果是nil,UIApplicationMain()函数会给系统创建一个UIApplication这个类的一个对象,而且是唯一的,叫做应用程序对象

    delegateClassName:UIApplicationMain()使用给定类创建代理并给应用程序设置代理

    UIApplicationMain()这个函数,开启事件循环

2、UIApplicationDelegate

UIApplicationDelegate是?个OC的协议。??声明了?堆?法,这些? 法都与应?程序运?状态有关,它们由应?程序代理实现。UIApplication 对象负责调?。

iOS生命周期:

程序启动:

application:didFinishLaunchingWithOptions:

applicationDidBecomeActive:

挂起:

applicationWillResignActive:     (tag:按两下Home键只发送此消息)

applicationDidEnterBackground:

恢复:

applicationWillEnterForeground:

applicationDidBecomeActive:     (tag:从此状态返回程序只发送此消息)

结束:

applicationWillResignActive:   

applicationDidEnterBackground:

AppDelegate applicationWillTerminate:

输出函数名:

    NSLog(@"%s", __FUNCTION__);


UI_02 基础视图、程序启动流程

标签:

原文地址:http://my.oschina.net/zooyf/blog/493650

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!