UIButton

#import "AppDelegate.h"


@interface AppDelegate () <UITextFieldDelegate>


@property (nonatomic,assign) BOOL flag;


// 将控件视图写成属性的好处就是在这个类内部可以随时获取对象取值赋值

@property (nonatomic,retain) UITextField *textField;


@end



@implementation AppDelegate


- (void)dealloc

{

    [_textField release];

    [_window 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];

    

    // 单行文本输入框

    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 20, 335, 40)];

    self.textField.backgroundColor = [UIColor yellowColor];

    self.textField.tag = 10000;

    

    

    // 设置textField的代理人

    _textField.delegate = self;

    

    

    [self.window addSubview:self.textField];

    [_textField release];

    

    self.textField.placeholder = @"请输入用户名";

    

    self.textField.borderStyle = UITextBorderStyleRoundedRect;

    

    self.textField.clearButtonMode = UITextFieldViewModeAlways;

    

    // textField.secureTextEntry = YES;

    

    self.textField.keyboardType = UIKeyboardTypeURL;


    

    

    // UIButton

    //使用button的遍历构造器的方法进行创建, 便利构造器,不用释放内存

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

    // title

    button.backgroundColor = [UIColor yellowColor];

    //位置

    button.frame = CGRectMake(20, 120, 335, 40);

    [self.window addSubview:button];

    

    //button添加标题

    [button setTitle:@"title" forState:UIControlStateNormal];

    

    

    //

    button.showsTouchWhenHighlighted = YES;

    

    // button指定一个功能(方法)

    

    // 参数1: 点击按钮之后,让什么对象调用方法

    // 参数2: 让参数1的对象调用的方法

    // 参数3: 按钮的事件(点击,拖动....)

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

    

    //button设置图片  //图片名为"zz.png"

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

    //setBackgroundImage方法会把图片铺满整个button

    

    //隐藏多余的部分

    button.layer.masksToBounds = YES;

    

    

    UIButton *switchButton = [UIButton buttonWithType:UIButtonTypeCustom];

    switchButton.frame = CGRectMake(20, 300, 50, 50);

    

    //添加图片

    //如果要使用setImage,button类型必须是Custom

    [switchButton setImage:[UIImage imageNamed:@"zz.png"] forState:UIControlStateNormal];

    [self.window addSubview:switchButton];

    [switchButton addTarget:self action:@selector(changeImage:) forControlEvents:UIControlEventTouchUpInside];

    

    

    

    

    return YES;

}


- (void)changeImage:(UIButton *)btn

{



}





// 实现textField的协议方法

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

{

 // 当用户点击textField的时候,要不要开始编辑状态

    return YES;

}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField

{

    // 是否允许结束编辑状态

    return YES;


}


- (void)textFieldDidBeginEditing:(UITextField *)textField

{

textField.text = @"不要乱写啊啊啊啊啊";

    NSLog(@"已经开始编辑");


}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

{

    return YES;

}


- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

    

    return YES;

}





// 按钮点击方法的写法格式

// - (void)方法名:(UIButton *)btn

- (void)buttonAction:(UIButton *)btn

{

    NSLog(@"点击了");

    //1.

    

//    if (self.window.backgroundColor == [UIColor whiteColor]) {

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

//    }else{

//    

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

//    }

    

    // 2.用属性

//    if (self.flag == NO) {

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

//    }else{

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

//    }

//    self.flag = !self.flag;

    

    

    // 父类的指针可以指向子类的对象,反过来不可以

    //用标记

//    UITextField *field = (UITextField *)[self.window viewWithTag:10000];

//    if ([field.text isEqualToString:@"red"]) {

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

//    }

    

    //用属性

    if ([_textField.text isEqualToString:@"red"]) {

        _textField.text = @"blue";

        self.window.backgroundColor = [UIColor blueColor];

        

    }

    

    // textField键盘回收

    [_textField resignFirstResponder];

    

   


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值