6-2作业

6-2作业

如果答错了,重新开始,再点下面灰色按钮,按钮不会隐藏,不知道错在哪



//

//  ViewController.m

//  Game

//

//  Created by 李姝谊 on 2018/8/16.

//  Copyright © 2018年 李姝谊. All rights reserved.

//

/*

 模型赋值

 正确获取到数据之后,如果想对标题,答案等进行赋值,可通过如下方法

 1 获取对应的model

 //取出对应的model,index为0到9

 IdiomModel *model= self.questions[self.index];

 2 进行对应的赋值

 给title赋值就可以通过model.title获取

 给answer赋值就可以通过model.answer获取

 想要获取options选项的个数可以通过model.options.count获取

 */





#import "ViewController.h"

#import "IdiomModel.h"


@interface ViewController () {

    int index;

    int money;

}

//模型数组

@property(strong,nonatomic)NSArray *questions;


@property (weak, nonatomic) IBOutlet UIView *answerView;


@property (weak, nonatomic) IBOutlet UIButton *imageButton;

@property (weak, nonatomic) IBOutlet UILabel *label1;

@property (weak, nonatomic) IBOutlet UILabel *label2;

@property (weak, nonatomic) IBOutlet UIButton *showMoney;

@property (weak, nonatomic) IBOutlet UIView *view1;





@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    money = 10000;

    [self gameImage];

    [self answerButton];

    [self blueViewInit];

    [self fourButton];

    [self label];

    [self showTheMoney];

    

}



- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}




//答案区按钮

-(void)answerButton {

   

    IdiomModel *model= self.questions[index];

    

    for (int i = 0;i <= 20;i++) {

        

        UIButton *b = [[UIButton alloc]initWithFrame:CGRectMake(15 + 50*(i%7), 10+ 50*(i/7), 44, 44)];

        [b setBackgroundImage:[UIImage imageNamed:@"btn_option_highlighted"] forState:UIControlStateNormal];

        [b addTarget:self action:@selector(clickOptionButton:) forControlEvents:UIControlEventTouchUpInside];

        [b setTitle:model.options[i] forState:UIControlStateNormal];

        [self.view1 addSubview:b];

        

    }

    


}


-(void)blueViewInit {

    //在创建之前答案区的所有按钮都要删掉

    for (UIButton *btn in self.answerView.subviews) {

        [btn removeFromSuperview];

    }

    //根据答案的长度来for循环遍历创建空格按钮

    IdiomModel *model= self.questions[self->index];

    for (int i = 0; i < model.answer.length; i++) {

        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake((i+1)*280/(model.answer.length), 0, 44, 44)];

        button.backgroundColor = [UIColor whiteColor];

        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        button.tag = i;

        [button addTarget:self action:@selector(rollback:) forControlEvents:UIControlEventTouchUpInside];

        [self.answerView addSubview:button];

    }

}


-(void)rollback:(UIButton *)btn {

    

    NSString *font = [btn currentTitle];

    for (UIButton *bt in self.view1.subviews) {

        if ([bt currentTitle] == font) {

            bt.hidden = NO;

            [btn setTitle:nil forState:UIControlStateNormal];

        }

    }

}




//重写get方法

-(NSArray *)questions {

    //判断

    if (_questions == nil) {

        //如果没有就加载,加载plist文件的方法

        NSString *path = [[NSBundle mainBundle]pathForResource:@"questions" ofType:@"plist"];

        //因为plist文件是数组类型,所有需要创建一个数组,保存从plist获取到的数据

        NSArray *dictArr = [NSArray arrayWithContentsOfFile:path];

        

        //创建可变数组,保存从数组dictArr遍历出来的每一个元素

        NSMutableArray *mutArr = [NSMutableArray array];

        

        //循环字典转模型,将遍历出来的每一个元素,再赋值给对应的模型属性

        for (NSDictionary *dict in dictArr) {

            //创建对象

            IdiomModel *model = [[IdiomModel alloc]init];

            model.answer = dict[@"answer"];

            model.title = dict[@"title"];

            model.options = dict[@"options"];

            

            //添加到可变数组

            [mutArr addObject:model];

        }

        //循环完毕赋值给属性

        _questions = mutArr;

        

    }

    return _questions;

}


- (void)clickOptionButton:(UIButton *)sender{

    //获取当前点击按钮的文字

    NSString *font = [sender currentTitle];

    //设置给答案区域的空白按钮

    for (UIButton *btn in self.answerView.subviews) {

        //如果为空 就把文字设置上去

        if ([btn currentTitle]==nil) {

            //设置文字

            [btn setTitle:font forState:UIControlStateNormal];

            

            //隐藏自己

            sender.hidden=YES;//设置隐藏

            break;//直接跳出方法

        }

    }

    

    //接下来判断答案区是否填满,填满以后就判断答案是否正确

    UIButton *lastButton = self.answerView.subviews.lastObject;

    if ([lastButton currentTitle] != nil ) {

        NSLog(@"满了");

        NSString *temp = @"";

        for (UIButton *btn in self.answerView.subviews){

            NSString *str = [btn currentTitle];

            

            temp = [temp stringByAppendingString:str];

        }

       

        IdiomModel *model= self.questions[self->index];

        if ([temp isEqualToString:model.answer]) {

            

            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"恭喜你答对了" delegate:self cancelButtonTitle:@"点击取消" otherButtonTitles:nil, nil];

            [alertView show];

            money += 10000;

            [self viewDidLoad];

        } else {

            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"你答错了哦" delegate:self cancelButtonTitle:@"跳过" otherButtonTitles:@"再来一次", nil];

            [alertView show];

            


            

            money -= 1000;

            [self viewDidLoad];

        }

    }

}


-(void)nextOne {

    index++;

    do {

        [self viewDidLoad];

    } while (index==9);

    

}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    if (buttonIndex == 0) {

        

        [self nextOne];

        

       

    }

    

    if (buttonIndex == 1) {

        

        [self viewDidLoad];

        

    }

}


-(void)gameImage {

    self.imageButton.layer.borderColor = [UIColor grayColor].CGColor;

    self.imageButton.layer.borderWidth = 1.0;

    self.imageButton.layer.cornerRadius = 3.0;

    

    int n = index + 1;

    NSString *name = [NSString stringWithFormat:@"%d",n];

    name = [name stringByAppendingString:@".jpeg"];

    

    UIImage *back = [UIImage imageNamed:name];

    [self.imageButton setBackgroundImage:back forState:UIControlStateNormal ];

    

    [self.imageButton addTarget:self action:@selector(enlarge:) forControlEvents:UIControlEventTouchUpInside];

}


-(void)fourButton {

    UIButton *pointButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 160, 70, 30)];

    [pointButton setImage:[UIImage imageNamed:@"icon_tip"] forState:UIControlStateNormal];

    [pointButton setBackgroundImage:[UIImage imageNamed:@"btn_left"] forState:UIControlStateNormal];

    [pointButton setBackgroundImage:[UIImage imageNamed:@"btn_left_highlighted"] forState:UIControlStateHighlighted];

    [pointButton setTitle:@"提示" forState:UIControlStateNormal];

    [self.view addSubview:pointButton];

    

    

    UIButton *helpButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 260, 70, 30)];

    [helpButton setImage:[UIImage imageNamed:@"icon_help"] forState:UIControlStateNormal];

    [helpButton setBackgroundImage:[UIImage imageNamed:@"btn_left"] forState:UIControlStateNormal];

    [helpButton setBackgroundImage:[UIImage imageNamed:@"btn_left_highlighted"] forState:UIControlStateHighlighted];

    [helpButton setTitle:@"帮助" forState:UIControlStateNormal];

    [self.view addSubview:helpButton];

    

    UIButton *enlargeButton = [[UIButton alloc]initWithFrame:CGRectMake(305, 160, 70, 30)];

    [enlargeButton setImage:[UIImage imageNamed:@"icon_img"] forState:UIControlStateNormal];

    [enlargeButton setBackgroundImage:[UIImage imageNamed:@"btn_right"] forState:UIControlStateNormal];

    [enlargeButton setBackgroundImage:[UIImage imageNamed:@"btn_right_highlighted"] forState:UIControlStateHighlighted];

    [enlargeButton setTitle:@"大图" forState:UIControlStateNormal];

    [enlargeButton addTarget:self action:@selector(enlarge:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:enlargeButton];

    

    UIButton *nextButton = [[UIButton alloc]initWithFrame:CGRectMake(305, 260, 70, 30)];

    [nextButton setBackgroundImage:[UIImage imageNamed:@"btn_right"] forState:UIControlStateNormal];

    [nextButton setBackgroundImage:[UIImage imageNamed:@"btn_right_highlighted"] forState:UIControlStateHighlighted];

    [nextButton setTitle:@"下一题" forState:UIControlStateNormal];

    [nextButton addTarget:self action:@selector(nextOne) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:nextButton];

}


-(void)enlarge:(UIButton *)bt {

    if (self.imageButton.frame.size.height == 175 ) {

        self.imageButton.frame = CGRectMake(0, 100, 375, 375);

        

    }else{

        self.imageButton.frame = CGRectMake(100, 140, 175, 175);

    }

}


-(CGRect)imageRectForContentRect:(CGRect)contentRect {

    CGFloat w = 15;

    CGFloat h = 20;

    CGFloat x = 3;

    CGFloat y = (contentRect.size.height - h)/2;

    return CGRectMake(x, y, w, h);

}


-(void)label {

    IdiomModel *model= self.questions[self->index];

    self.label1.text = model.title;

    self.label1.textColor = [UIColor whiteColor];

    self.label1.textAlignment = NSTextAlignmentCenter;

    

    self.label2.text = [NSString stringWithFormat:@"%d/10",index+1];

    self.label2.textColor = [UIColor whiteColor];

    self.label2.textAlignment = NSTextAlignmentCenter;

    

}


-(void)showTheMoney {

    

    [self.showMoney setTitle:[NSString stringWithFormat:@"%d",money] forState:UIControlStateNormal];

    [self.showMoney setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    [self.showMoney setImage:[UIImage imageNamed:@"coin"] forState:UIControlStateNormal];

    

   

}

@end



正在回答 回答被采纳积分+1

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

2回答
Tender10 2018-08-23 14:32:07

你的index没有给初始值,IdiomModel *model= self.questions[index];是无法获取到对应的model值的。你的答案按钮还有选项按钮以及图片能够显示嘛?因为看不见你的storyboard,所以没办法验证你的代码。你可以提交你的作业,并且在作业中写出你对于目前作业的一个疑问,都会给你详细的进行批改,并解答。

  • 提问者 lsyyyy #1
    初始不是默认0么,都可以显示的,我先提交之后您帮我看看吧
    2018-08-23 14:34:03
  • Tender10 回复 提问者 lsyyyy #2
    好的,你可以标注你遇到的作业问题。
    2018-08-23 16:09:58
Tender10 2018-08-23 11:01:20

你可以将你的运行效果截图贴出来嘛。首先答错以后,可以先将答案按钮答错的文字点击一下,清空答案按钮的文字,并让选项按钮重新显示,然后再进行选择,如果答案区域的按钮已经填满,下面的选项按钮点击是没有反应的。

  • 提问者 lsyyyy #1
    答错之后我会重新运行view did load ,view did load里面有一个【self blueViewInit】这个方法里给所有的按钮都删除重新初始化了啊
    2018-08-23 11:34:29
  • 提问者 lsyyyy #2
    而且就是我点完灰色按钮它不会消失,会变成另一个字(从第二张图开始,第一张图正常)
    2018-08-23 11:44:42
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

0 星

相似问题

登录后可查看更多问答,登录/注册

iOS入门:基础语法与常用控件
  • 参与学习       1337    人
  • 提交作业       712    份
  • 解答问题       1878    个

很多人都在疑问没有基础该怎么学习iOS开发?不用担心,本路径采用游戏关卡式的教学模式,并且以经典OC与最新Swift双重结合的教学内容,帮助大家快速掌握两种iOS开发语言基础,去掉“零”标签。

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

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