UIBUTTON点击事件

来源:互联网 发布:java ee tag标签文件 编辑:程序博客网 时间:2024/05/22 02:22

#import "Person.h"

@implementation AppDelegate

- (void)dealloc

{

    [_arrrelease];

    [self.windowrelease];

    [superdealloc];

}


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

{

    self.window = [[[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]]autorelease];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColorwhiteColor];

    [self.windowmakeKeyAndVisible];

   /*

     UIButton

     */

    //按钮实例化

    UIButton *b = [UIButtonbuttonWithType:UIButtonTypeDetailDisclosure];

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

    

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

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

    // 修改标题颜色

    [b setTitleColor:[UIColorblackColor] 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 = [NSArrayarrayWithObjects:@"1",@"2",@"3",nil];

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

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

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

  


    

   

    [self.windowaddSubview:b];

    return YES;

}


bool flag =0;

-(void)click

{

    NSLog(@"click");

   if (flag) {

       self.window.backgroundColor = [UIColorblackColor];

        

    }else{

       self.window.backgroundColor = [UIColorwhiteColor];

    }

   flag = !flag;

    

}


0 0