UI 02 UIButton
2015-08-03 08:41
399 查看
UIButton 继承于 UIControl , UIControl 继承于 UIView.
- (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 purpleColor]; [self.window makeKeyAndVisible]; [self.window release]; // 创建一个UIButton UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; button.frame = CGRectMake(150, 100, 80, 80) ; button.backgroundColor = [UIColor whiteColor]; [self.window addSubview:button]; [button setTitle:@"确认" forState:UIControlStateNormal]; button.titleLabel.font = [UIFont systemFontOfSize:25]; button.layer.cornerRadius = 8; button.layer.masksToBounds = YES; [button addTarget:self action:@selector(changePic:) forControlEvents:UIControlEventTouchUpInside]; button.tag = 1000; self.isSelected = YES; // 给Button设置背景图片 [button setBackgroundImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal]; UIButton *buttontwo = [UIButton buttonWithType:UIButtonTypeCustom]; buttontwo.frame = CGRectMake(150, 220, 80, 80); [self.window addSubview:button1]; // buttontwo.layer.cornerRadius = 8; // [buttontwo setTitle:@"测试" forState:UIControlStateNormal]; // buttontwo.titleLabel.font = [UIFont systemFontOfSize:10]; [buttontwo addTarget:self action:@selector(changeImage:) forControlEvents:UIControlEventTouchUpInside]; buttontwo.tag = 1001; // 设置前景图片 [buttontwo setImage:[UIImage imageNamed:@"BtnOff.png"] forState:UIControlStateNormal]; self.isClick = NO; return YES; } - (void)click:(UIButton *)button{ //谁触发了按钮,相应的button就是那个对象. // 1.不管按谁,只修改的是tag是1000按钮的内容 UIButton *but = (UIButton *)[self.window viewWithTag:1000]; // 判断当前按钮的标题 //currentTitle -- 获取当前按钮标题. if ([but.currentTitle compare:@"确认"] == 0) { [but setTitle:@"取消" forState:UIControlStateNormal]; }else{ [but setTitle:@"确认" forState:UIControlStateNormal]; } // 打印出button当前的标题. NSLog(@"%@",but.currentTitle); //2.按哪个按钮,哪个按钮的内容就会改变 if ([button.currentTitle isEqualToString:@"确认"]) { [button setTitle:@"取消" forState:UIControlStateNormal]; }else{ [button setTitle:@"确认" forState:UIControlStateNormal]; } NSLog(@"%ld",button.tag); 定义一条 @property(nonatomic, assign)BOOL isSelected; 的属性 // 更换按钮的背景图方法. - (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; if ([button.currentTitle isEqualToString:@"确认"]) { [button setTitle:@"取消" forState:UIControlStateNormal]; }else{ [button setTitle:@"确认" forState:UIControlStateNormal]; } } } 定义一条@property(nonatomic, assign)BOOL isClick;的属性. - (void)changeImage:(UIButton *)button{ //更换前景图片 if (self.isClick) { [button setImage:[UIImage imageNamed:@"BtnOff.png"] forState:UIControlStateNormal]; }else{ [button setImage:[UIImage imageNamed:@"BtnOn.png"] forState:UIControlStateNormal]; } self.isClick = !self.isClick; }
相关文章推荐
- iOS UI03_登陆+注册 UI1~3系统归纳
- UIViewController视图控制器(移动self.view,随机颜色)
- UIImageView图片填充三种模式的区别
- HackerRank - "Snakes and Ladders: The Quickest Way Up"
- 为什么不推荐用UniversalImageLoader加载res/drawable
- UE4信息交互-关卡蓝图引用关卡中的对象
- wildfly使用入门
- IOS开发UI篇--IOS动画(Core Animation)总结
- 本教程针对HBuilder5.0.0,制作日期2014-12-31(从HBuilder工具上获得)
- UE4信息交互-命令行执行自定义事件ConsoleCommand
- 本教程针对HBuilder5.0.0,制作日期2014-12-31(从HBuilder工具上获得)
- [leedcode 187] Repeated DNA Sequences
- leetcode 63: Unique Paths II
- iOS开发——UI进阶篇(十三)UITabBarController简单使用,qq主流框架
- hdu 5312 Sequence
- php图像函数 imagecreatetruecolor()和imagecreate()的区别
- 关于Android Notification.Builder不显示通知的问题
- iOS学习笔记5-GCDAsyncUdpSocket中的dispatch_queue_set_specific
- iOS多线程编程之NSOperation和NSOperationQueue的使用
- Android多线程中UI线程和其他线程进行交互的几种方法小结