如何通过点击button4来改变button3的字体颜色?
//
// ViewController.m
// youxi
//
// Created by bb on 12/11/2018.
// Copyright © 2018 boe. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//获取视图坐标及大小
CGFloat x = self.view.frame.origin.x;
CGFloat y = self.view.frame.origin.y;
NSLog(@"x=%f y=%f",x,y);
NSLog(@"size%@",NSStringFromCGSize(self.view.frame.size));
//添加上半部分色块
UIView *cafe = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375, 400)];
cafe.backgroundColor = [UIColor darkGrayColor];
[self.view addSubview:cafe];
//添加得分lable
UILabel *score = [[UILabel alloc]initWithFrame: CGRectMake(300, 25, 75, 30)];
score.text = @"10000";
//score.backgroundColor = [UIColor grayColor];
score.textColor = [UIColor whiteColor];
score.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:score];
//添加硬币图片
UIImage *yingbi = [UIImage imageNamed:@"coin@2x.png"];
UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(285, 30, 20, 20)];
imageview.image = yingbi;
[self.view addSubview:imageview];
//添加页码
UILabel *yema = [[UILabel alloc]initWithFrame: CGRectMake(0, 50, 375, 30)];
yema.text = @"1/10";
yema.textColor = [UIColor whiteColor];
yema.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:yema];
//添加标题
UILabel *biaoti = [[UILabel alloc]initWithFrame: CGRectMake(0, 80, 375, 30)];
biaoti.text = @"恶搞风格的喜剧大片";
biaoti.textColor = [UIColor whiteColor];
biaoti.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:biaoti];
//调用添加按钮方法
[self button1];
[self button2];
[self button3];
[self button4];
}
//添加按钮1方法
-(void)button1{
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 110, 80, 40)];
[button setTitle:@"提示" forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"btn_left.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"btn_left_highlighted.png"] forState:UIControlStateHighlighted];
[self.view addSubview:button];
}
//添加按钮2方法
-(void)button2{
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(295, 110, 80, 40)];
[button setTitle:@"大图" forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"btn_right.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"btn_right_highlighted.png"] forState:UIControlStateHighlighted];
[self.view addSubview:button];
}
//添加按钮3方法
-(void)button3{
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 350, 80, 40)];
[button setTitle:@"帮助" forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"btn_left.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"btn_left_highlighted.png"] forState:UIControlStateHighlighted];
[self.view addSubview:button];
}
//添加按钮4方法
-(void)button4{
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(295, 350, 80, 40)];
[button setTitle:@"下一题" forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[UIImage imageNamed:@"btn_right.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"btn_right_highlighted.png"] forState:UIControlStateHighlighted];
[self.view addSubview:button];
}
-(void)buttonAction:(UIButton*)bt{
[bt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
NSLog(@"我被点击了");
}
@end
正在回答 回答被采纳积分+1
请问老师:
昨天我想到一个方法,就是通过self.view.subviews获取到视图上的元素再通过tag找到指定元素,请问这样操作可以吗?
//
// ViewController.m
// youxi
//
// Created by Ordineat on 12/11/2018.
// Copyright © 2018 bofu. All rights reserved.
//
#import "ViewController.h"
#import "Mybutton.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//获取视图坐标及大小
CGFloat x = self.view.frame.origin.x;
CGFloat y = self.view.frame.origin.y;
NSLog(@"x=%f y=%f",x,y);
NSLog(@"size%@",NSStringFromCGSize(self.view.frame.size));
//添加上半部分色块
UIView *cafe = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375, 400)];
cafe.backgroundColor = [UIColor darkGrayColor];
[self.view addSubview:cafe];
//添加得分lable
UILabel *score = [[UILabel alloc]initWithFrame: CGRectMake(300, 25, 75, 30)];
score.text = @"10000";
//score.backgroundColor = [UIColor grayColor];
score.textColor = [UIColor whiteColor];
score.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:score];
//添加硬币图片
UIImage *yingbi = [UIImage imageNamed:@"coin@2x.png"];
UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(285, 30, 20, 20)];
imageview.image = yingbi;
[self.view addSubview:imageview];
//添加页码
UILabel *yema = [[UILabel alloc]initWithFrame: CGRectMake(0, 50, 375, 30)];
yema.text = @"1/10";
yema.tag = 2;
yema.textColor = [UIColor whiteColor];
yema.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:yema];
//添加标题
UILabel *biaoti = [[UILabel alloc]initWithFrame: CGRectMake(0, 80, 375, 30)];
biaoti.text = @"恶搞风格的喜剧大片";
biaoti.textColor = [UIColor whiteColor];
biaoti.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:biaoti];
//调用添加按钮方法
[self button1];
[self button2];
[self button3];
[self button4];
[self button6];
}
//自定义button6
-(void)button6{
Mybutton *button = [[Mybutton alloc]initWithFrame:CGRectMake(5, 25, 100, 30)];
[self.view addSubview:button];
}
//添加按钮1方法
-(void)button1{
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 110, 80, 40)];
[button setTitle:@"提示" forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"btn_left.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"btn_left_highlighted.png"] forState:UIControlStateHighlighted];
[self.view addSubview:button];
}
//添加按钮2方法
-(void)button2{
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(295, 110, 80, 40)];
[button setTitle:@"大图" forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"btn_right.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"btn_right_highlighted.png"] forState:UIControlStateHighlighted];
[self.view addSubview:button];
}
//添加按钮3方法
-(void)button3{
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 350, 80, 40)];
[button setTitle:@"帮助" forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"btn_left.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"btn_left_highlighted.png"] forState:UIControlStateHighlighted];
button.tag = 1;
[self.view addSubview:button];
}
//添加按钮4方法
-(void)button4{
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(295, 350, 80, 40)];
[button setTitle:@"下一题" forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[UIImage imageNamed:@"btn_right.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"btn_right_highlighted.png"] forState:UIControlStateHighlighted];
[self.view addSubview:button];
}
//按钮4响应方法
-(void)buttonAction:(UIButton*)bt{
[bt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
NSLog(@"我被点击了");
NSArray *subViewArray = self.view.subviews;
for(UIButton *bt in subViewArray){
if(bt.tag == 1){
[bt setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
}
}
for(UILabel *Lb in subViewArray){
if(Lb.tag == 2){
NSString*string = Lb.text;
NSArray*array = [string componentsSeparatedByString:@"/"];//从字符/中分隔成2个元素的数组
NSLog(@"array:%@",array);
NSLog(@"array1 ==== %@",[array firstObject]);
NSLog(@"array2 ==== %@",[array lastObject]);
int aaa = [[array firstObject] intValue]+1;
Lb.text = [NSString stringWithFormat:@"%d/10",aaa];
}
}
}
@end
- 参与学习 1337 人
- 提交作业 712 份
- 解答问题 1878 个
很多人都在疑问没有基础该怎么学习iOS开发?不用担心,本路径采用游戏关卡式的教学模式,并且以经典OC与最新Swift双重结合的教学内容,帮助大家快速掌握两种iOS开发语言基础,去掉“零”标签。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星