UIBUTTON点击事件

#import "Person.h"

@implementation AppDelegate

- (void)dealloc

{

    [_arr release];

    [self.window release];

    [super dealloc];

}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    /*

     UIButton

     */

    //按钮实例化

    UIButton *b = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    b.frame = CGRectMake(0, 20, 50, 50);

    

    // 给按钮设置标题(普通状态下)

    [b setTitle:@"1" forState:UIControlStateNormal];

    // 修改标题颜色

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

    // 高亮状态下的标题

    [b setTitle:@"2" forState:UIControlStateHighlighted];

    // 按钮不可被点击的状态

    [b setTitle:@"3" forState:UIControlStateDisabled];

    //按钮不可接受点击事件

//    b.enabled = NO;

    //按钮的事件机制

    //添加点击事件

    /*

     UIControlState     状态

     UIControlEvents    事件

     改变空间的状态必须有事件

     */

    //UIControlEventTouchUpInside 按下并且抬起

    //必须要实现@selector方法 不实现崩溃

    /*

     bUIControlEventTouchUpInside(按下并抬起)事件时 会在self当前对象中调用@selector的方法

     */

    //成员变量不能使用加方法

    /*

    _arr =@[@"123",@"12341"];

    _arr =[[[NSArray alloc] initWithObjects:@"1",@"2",@"3", nil] autorelease];

     */

    //正确写法

    _arr = [NSArray arrayWithObjects:@"1",@"2",@"3", nil];

    _arr = [[[NSArray alloc] initWithObjects:@"1",@"2",@"3", nil] retain];

    //Person *p =[[Person alloc] init]; addTarget调用Person对象的话,会触发Personclick方法

    [b addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];

  


    

   

    [self.window addSubview:b];

    return YES;

}


bool flag = 0;

-(void)click

{

    NSLog(@"click");

    if (flag) {

        self.window.backgroundColor = [UIColor blackColor];

        

    }else{

        self.window.backgroundColor = [UIColor whiteColor];

    }

    flag = !flag;

    

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值