如何通过点击button4来改变button3的字体颜色?

如何通过点击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

登陆购买课程后可参与讨论,去登陆

2回答
提问者 我是大英雄 2018-11-14 22:51:08

请问老师:

昨天我想到一个方法,就是通过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


  • 通过这种方式找到对应的视图,然后再去改变颜色的思路也是可以的。
    2018-11-15 09:46:32
  • 提问者 我是大英雄 回复 Tender10 #2
    谢谢老师! ;)
    2018-11-16 00:31:05
Tender10 2018-11-14 09:52:19

这个可以实现的哦,首先将button3和button4对象,定义为全局对象,如下在@interface中,通过@property定义

@interface ViewController ()

@property(nonatomic,strong)UIButton *button3;

@property(nonatomic,strong)UIButton *button4;

@end

然后在点击button4的响应方法中,通过这个全局的button3对象去设置字体颜色就可以了。

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师