标签:
#import "AppDelegate.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 创建window self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; self.window.rootViewController = [[ViewController alloc] init]; #pragma mark - UIButton // 1.创建对象并初始化 UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; // 2.设置属性 button.frame = CGRectMake(50, 100, 250, 250); //button.backgroundColor = [UIColor orangeColor]; button.layer.cornerRadius = 10; // 边框圆角 button.layer.borderWidth = 2; // 边框边线宽度 //button.tintColor = [UIColor blackColor]; // 设置字体颜色 [button setTitle:@"点我啊" forState:UIControlStateNormal]; // 设置字体 [button setTitle:@"点你咋地" forState:UIControlStateHighlighted]; [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; // 设置字体颜色 UIImage *image1 = [UIImage imageNamed:@"biaoqingdi副本.png"]; [button setBackgroundImage:image1 forState:UIControlStateNormal]; // 设置背景图片 [button setBackgroundImage:[UIImage imageNamed:@"chiniupa副本"] forState:UIControlStateHighlighted]; [button setImage:[UIImage imageNamed:@"222"] forState:UIControlStateNormal]; // 设置前景图片(必须是镂空图片) // 3.添加事件 // 单击状态下 [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; // 4.添加到父视图 [self.window addSubview:button]; return YES; } // 实现按钮点击事件 - (void)click:(UIButton *)sender { NSLog(@"哈哈哈"); sender.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1]; // 移除事件 [sender removeTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; } @end
标签:
原文地址:http://www.cnblogs.com/zhizunbao/p/5360096.html