码迷,mamicode.com
首页 > 移动开发 > 详细

iOS UI02_UIButton

时间:2015-07-31 09:04:52      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:ui   uibutton   ios   

//

//  AppDelegate.m

//  UI02_UIButton

//

//  Created by dllo on 15/7/29.

//  Copyright (c) 2015 zhozhicheng. All rights reserved.

//


#import "AppDelegate.h"


@interface AppDelegate ()

@property(nonatomic,assign)BOOL isSelected;

@property(nonatomic,assign)BOOL isClick


@end


@implementation AppDelegate

-(void)dealloc

{

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

//    //Button自己的便利构造器的方式来创建对象

//    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

//    //指定button位置和大小

//    button.frame=CGRectMake(100, 100, 100, 40);

//    //设置背景颜色

//    button.backgroundColor=[UIColor yellowColor];

//    [self.window addSubview:button];

//    //button不需要release

//    //button设置标题

//    [button setTitle:@"抽奖" forState:UIControlStateNormal];

//    [button setTitle:@"恭喜中奖" forState:UIControlStateHighlighted];

//    //设置标题字体大小

//    button.titleLabel.font=[UIFont systemFontOfSize:20];

//    //设置圆角,(直接拿掉多余的)

//    button.layer.cornerRadius=10;

//    //设置边框

//    button.layer.borderWidth=1;

//    //点击方法 button最重要的方法

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

    //重新创建一个Button

    UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];

    button.frame=CGRectMake(80, 300, 100, 40);

//    button.backgroundColor=[UIColor yellowColor];

    [self.window addSubview:button];

//    [button setTitle:@"确认" forState:UIControlStateNormal];

//    button.layer.borderWidth=1;

//    button.layer.cornerRadius=10;

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

    //设置一个tag

    button.tag=1000;

    //button设置背景图片

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

    UIButton *button1=[UIButton buttonWithType:UIButtonTypeCustom];

    button1.frame=CGRectMake(220, 300, 100, 40);

//    button1.backgroundColor=[UIColor redColor];

    [self.window addSubview:button1];

    [button1 setTitle:@"注册" forState:UIControlStateNormal];

//    button1.layer.borderWidth=1;

    button1.layer.cornerRadius=10;

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

    button1.tag=1001;

    self.isSelected =YES;

    //如果想使用setImage设置图片的话,button的类型要调整成custom,setImage方法不会把图片方达成按钮大小

    [button1 setImage:[UIImage imageNamed:@"BtnOff.png"] forState:UIControlStateNormal];

    self.isClick=YES;

    return YES;

    

}


-(void)changePic:(UIButton *)button{

    if (self.isSelected) {

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

    }else{

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

    }

    //把当前的状态进行调整

    self.isSelected =!self.isSelected;

}

-(void)changeImage:(UIButton *)button1{

    if (self.isClick ) {

        [button1 setImage:[UIImage imageNamed:@"BtnOn.png"] forState:UIControlStateNormal];

    }else{

        [button1 setImage:[UIImage imageNamed:@"BtnOff.png"] forState:UIControlStateNormal];

    }self.isClick ^=1;

}



-(void)click:(UIButton *)button{

    

//    UIButton *but=(UIButton *)[self.window viewWithTag:1000];

//    // 判断当前按钮的标题

//    //currentTitle获取当前按钮的标题

//    NSLog(@"%@",but.currentTitle);

//    if ([button.currentTitle isEqualToString:@"确认"]) {

//        [but setTitle:@"取消" forState:UIControlStateNormal];

//    }else{

//        [but setTitle:@"确认" forState:UIControlStateNormal];

//    }

    // 按钮的点击方法会传过来一个相应类型的button,谁触发了这个点击方法,相应的button就是哪个对象

//    NSLog(@"%ld",button.tag);


    if ([button.currentTitle isEqualToString:@"确认"]) {

        [button setTitle:@"取消" forState:UIControlStateNormal];

    }else{

        [button setTitle:@"确认" forState:UIControlStateNormal];

    }

    

}


;技术分享

//-(void)click:(UIButton *)button{

//    NSLog(@"d测试按钮的点击方法");

//}



- (void)applicationWillResignActive:(UIApplication *)application {

    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}


- (void)applicationDidEnterBackground:(UIApplication *)application {

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}


- (void)applicationWillEnterForeground:(UIApplication *)application {

    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}


- (void)applicationDidBecomeActive:(UIApplication *)application {

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}


- (void)applicationWillTerminate:(UIApplication *)application {

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}


@end


版权声明:本文为博主原创文章,未经博主允许不得转载。

iOS UI02_UIButton

标签:ui   uibutton   ios   

原文地址:http://blog.csdn.net/cheng_xiansheng/article/details/47164659

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