UIButton(07-08-05)

//
//  AppDelegate.m
//  UI02_Button
//
//  Created by lanou3g on 17/8/4.
//  Copyright ? 2017年 lanou3g. All rights reserved.
//

#import "AppDelegate.h"
#import "RootViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    RootViewController *rootViewController = [[RootViewController alloc] init];
    self.window.rootViewController = rootViewController;
    
    return YES;
}

- (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

//
//  RootViewController.m
//  UI02_Button
//
//  Created by lanou3g on 17/8/4.
//  Copyright ? 2017年 lanou3g. All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()

@property (nonatomic, assign) NSInteger count;

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(100, 100, 100, 100);
    button.backgroundColor = [UIColor redColor];
    //設置button某狀態(tài)的標題
    [button setTitle:@"你好" forState:UIControlStateNormal];
    [button setTitle:@"我不好" forState:UIControlStateHighlighted];
    [button setTitle:@"被選中" forState:UIControlStateSelected];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    
    //
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(100, 350, 100, 100);
    button1.backgroundColor = [UIColor orangeColor];
//    [button1 setTitle:@"點唄" forState:UIControlStateNormal];
    [button1 setBackgroundImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(button1Action:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
}

- (void)button1Action:(UIButton *)button1 {
    self.count += 1;
    CGFloat x = button1.isSelected ? 100 : 200;
    button1.selected = !button1.isSelected;
    button1.frame = CGRectMake(x, 350, 100, 100);
    NSLog(@"點擊成功");
    if (10 == self.count) {
        [button1 setTitle:@"換個方式" forState:UIControlStateNormal];
        self.count = 0;
    }else {
        [button1 setTitle:@"點唄" forState:UIControlStateNormal];
    }
    
}

- (void)buttonAction:(UIButton *)button {
    CGFloat y = button.isSelected ? 100:200;
    button.selected = !button.isSelected;
    button.frame = CGRectMake(100, y, 100, 100);
    NSLog(@"別戳我");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末樟蠕,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子琐驴,更是在濱河造成了極大的恐慌,老刑警劉巖晤硕,帶你破解...
    沈念sama閱讀 217,657評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件邢滑,死亡現(xiàn)場離奇詭異,居然都是意外死亡侧蘸,警方通過查閱死者的電腦和手機撤嫩,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評論 3 394
  • 文/潘曉璐 我一進店門偎捎,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事茴她⊙胺鳎” “怎么了?”我有些...
    開封第一講書人閱讀 164,057評論 0 354
  • 文/不壞的土叔 我叫張陵丈牢,是天一觀的道長祭钉。 經(jīng)常有香客問我,道長己沛,這世上最難降的妖魔是什么慌核? 我笑而不...
    開封第一講書人閱讀 58,509評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮申尼,結果婚禮上垮卓,老公的妹妹穿的比我還像新娘。我一直安慰自己师幕,他們只是感情好粟按,可當我...
    茶點故事閱讀 67,562評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著霹粥,像睡著了一般钾怔。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上蒙挑,一...
    開封第一講書人閱讀 51,443評論 1 302
  • 那天,我揣著相機與錄音愚臀,去河邊找鬼忆蚀。 笑死,一個胖子當著我的面吹牛姑裂,可吹牛的內容都是我干的馋袜。 我是一名探鬼主播,決...
    沈念sama閱讀 40,251評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼舶斧,長吁一口氣:“原來是場噩夢啊……” “哼欣鳖!你這毒婦竟也來了?” 一聲冷哼從身側響起茴厉,我...
    開封第一講書人閱讀 39,129評論 0 276
  • 序言:老撾萬榮一對情侶失蹤泽台,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后矾缓,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體怀酷,經(jīng)...
    沈念sama閱讀 45,561評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,779評論 3 335
  • 正文 我和宋清朗相戀三年嗜闻,在試婚紗的時候發(fā)現(xiàn)自己被綠了蜕依。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,902評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖样眠,靈堂內的尸體忽然破棺而出友瘤,到底是詐尸還是另有隱情,我是刑警寧澤檐束,帶...
    沈念sama閱讀 35,621評論 5 345
  • 正文 年R本政府宣布辫秧,位于F島的核電站,受9級特大地震影響厢塘,放射性物質發(fā)生泄漏茶没。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,220評論 3 328
  • 文/蒙蒙 一晚碾、第九天 我趴在偏房一處隱蔽的房頂上張望抓半。 院中可真熱鬧,春花似錦格嘁、人聲如沸笛求。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽探入。三九已至,卻和暖如春懂诗,著一層夾襖步出監(jiān)牢的瞬間蜂嗽,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評論 1 269
  • 我被黑心中介騙來泰國打工殃恒, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留植旧,地道東北人。 一個月前我還...
    沈念sama閱讀 48,025評論 2 370
  • 正文 我出身青樓离唐,卻偏偏與公主長得像病附,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子亥鬓,可洞房花燭夜當晚...
    茶點故事閱讀 44,843評論 2 354

推薦閱讀更多精彩內容