首页 > 代码库 > UILabel && UIButton

UILabel && UIButton

一、效果展示

1. 启动界面只有一个按钮

技术分享

2. 点击按钮,显示文本信息

技术分享

二、 分析

1. 两个控件UILabel && UIButton

2. 点击按钮触动方法设置文本信息

三、 实现

1. 不加载Main.storyboard

2. APPDelegate.m

 1 // 2 //  AppDelegate.m 3 //  4.1-标签和按钮 4 // 5 //  Created by LinKun on 16/8/31. 6 //  Copyright © 2016年 Lkun. All rights reserved. 7 // 8  9 #import "AppDelegate.h"10 #import "ViewController.h"11 12 @interface AppDelegate ()13 14 @end15 16 @implementation AppDelegate17 18 19 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {20     // Override point for customization after application launch.21     // 创建窗口22     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];23     self.window.rootViewController = [[ViewController alloc] init];24     self.window.backgroundColor = [UIColor whiteColor];25     [self.window makeKeyAndVisible];26     27     return YES;28 }29 30 - (void)applicationWillResignActive:(UIApplication *)application {31     // 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.32     // 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.33 }34 35 - (void)applicationDidEnterBackground:(UIApplication *)application {36     // 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.37     // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.38 }39 40 - (void)applicationWillEnterForeground:(UIApplication *)application {41     // 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.42 }43 44 - (void)applicationDidBecomeActive:(UIApplication *)application {45     // 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.46 }47 48 - (void)applicationWillTerminate:(UIApplication *)application {49     // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.50 }51 52 @end

 

3. ViewController.m

 1 // 2 //  ViewController.m 3 //  4.1-标签和按钮 4 // 5 //  Created by LinKun on 16/8/31. 6 //  Copyright © 2016年 Lkun. All rights reserved. 7 // 8  9 #import "ViewController.h"10 11 @interface ViewController ()12 13 @property (strong, nonatomic) UILabel *label;14 15 @end16 17 @implementation ViewController18 19 - (void)viewDidLoad {20     [super viewDidLoad];21     // Do any additional setup after loading the view, typically from a nib.22     23     // 屏幕尺寸24     CGRect screen = [[UIScreen mainScreen] bounds];25     26     // 标签位置尺寸27     CGFloat labelWidth = 90;28     CGFloat labelHeight = 20;29     CGFloat labelTopView = 150;30     31     // 创建标签32     self.label = [[UILabel alloc] initWithFrame:CGRectMake((screen.size.width - labelWidth)/2,33                                                                labelTopView,34                                                                labelWidth,35                                                                labelHeight)];36     self.label.textAlignment = NSTextAlignmentCenter;37     [self.view addSubview:self.label];38     39     // 按钮位置尺寸40     CGFloat buttonWidth = 60;41     CGFloat buttonHeight = 20;42     CGFloat buttonTopView = 240;43     44     // 创建按钮45     UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];46     [button setTitle:@"OK" forState:UIControlStateNormal];47     48     49     button.frame = CGRectMake((screen.size.width - buttonWidth)/2,50                               buttonTopView,51                               buttonWidth,52                                buttonHeight);53     // 点击按钮触动方法54     [button addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];55     [self.view addSubview:button];56     57 }58 59 #pragma mark 按钮方法60 - (void)onClick:(id)sender {61     self.label.text = @"onClick";62 }63 64 @end

 

UILabel && UIButton