2

编辑:伙计们,你们太棒了,谢谢所有的回复。我决定采用 stringByAppendingString 路线,因为它似乎是最简单的,但由于某种原因它仍然无法正常工作!如果这太过分了,请再次原谅我。非常感谢大家的支持!

这是一段代码:

    #import "ViewController.h"
    @interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupGame];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
  int finalorder = 12;
- (IBAction)buttonPressed: (UIButton*) button {
    if (button.tag == 1)
    {
        int order = 1;
    }
     if (button.tag == 2)
    {
        [order stringByAppendingString:@"2"];
    }
    if ([order isEqualToString:finalorder])
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Wrong!"
                            message:[NSString stringWithFormat:@"Try again!"]
                            delegate:self
                            cancelButtonTitle:@"Try"
                            otherButtonTitles:nil];
        [alert show];
                              };

    }

// 原始问题:我很抱歉,因为我是一个完全的菜鸟,这是一个菜鸟问题。

我正在制作我的第一个 iPhone 应用程序,我希望它成为一个音阶训练器——供学习音阶的人使用。屏幕上有一个八度的钢琴(12 个按钮,每个键一个),我希望程序跟踪用户按下按钮的顺序,以了解他是否确实记住了特定键的顺序规模。例如,我们在 C 级,键有:C、D、E、F、G、A、B。如果用户按 C,然后按 D,它会进一步工作。但是如果用户按 C,然后按 C#,应该会有一个警告说:“错了!再试一次”。

除此之外,我希望在标签中查看每个键的名称:如果用户按下 C 键,则有一个 C 等等。

那么如何跟踪压制的顺序呢? 到目前为止,我已经编写了下一段代码,它显示了键的名称并使用了计时器。非常感谢你的帮助!

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupGame];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)buttonPressed: (UIButton*) button {

    if (button.tag == 1)
    {
        waitinglabel.text = @"C";
    }
    else
    {
    [timer invalidate];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Wrong!"
                message:[NSString stringWithFormat:@"Try again!"]
                delegate:self
                cancelButtonTitle:@"Try"
                otherButtonTitles:nil];
    [alert show];
    }
    if (button.tag == 2)
    {
        waitinglabel.text = @"C#";
    }

    if (button.tag == 3)
    {
        waitinglabel.text = @"D";
    }


    if (button.tag == 4)
    {
        waitinglabel.text = @"D#";
    }

    if (button.tag == 5)
    {
        waitinglabel.text = @"E";
    }

    if (button.tag == 6)
    {
        waitinglabel.text = @"F";
    }

    if (button.tag == 7)
    {
        waitinglabel.text = @"F#";
    }

    if (button.tag == 8)
    {
        waitinglabel.text = @"G";
    }

    if (button.tag == 9)
    {
        waitinglabel.text = @"G#";
    }

    if (button.tag == 10)
    {
        waitinglabel.text = @"A";
    }

    if (button.tag == 11)
    {
        waitinglabel.text = @"A#";
    }

    if (button.tag == 12)
    {
        waitinglabel.text = @"B";
    }
}

- (void)setupGame {
    seconds = 30;
    count = 0;

    timerlabel.text = [NSString stringWithFormat:@"Time: %i", seconds];

    timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
                                             target:self
                                           selector:@selector(subtractTime)
                                           userInfo:nil
                                            repeats:YES];
}

- (void)subtractTime {
    seconds--;
    timerlabel.text = [NSString stringWithFormat:@"Time: %i",seconds];

    if (seconds == 0) {
        [timer invalidate];
    }
}

@end
4

5 回答 5

2

You have a predefined set of musical notes. You need to first create a unique notation for each note. Create a array of all notes.

Each note will have following info

[
  {
    "Tag":1,
    "Name":"C",
    "Info":{}
  },
  {
    "Tag":2,
    "Name":"C#",
    "Info":{}
  }
]

By creating an array like this you can remove tag comparison in your buttonPressed: method. You need to assign the tag of the note to the respective button. When a button is pressed using the tag of the button you can find out which note was pressed.

Then you can form the valid order in which a scale is formed using these notes.

Scale C = [1,3,5,6,8,10,12];

You need to keep an instance variable which will keep the track of which note user has entered, each time a button is pressed you would increment it. Upon each event using this index check the tags of notes stored in scale array. If there is a mismatch give an alert.

于 2013-04-29T15:41:32.570 回答
2

我会使用 NSMutableArray ,每次按下按钮时,只需将按钮的标签添加到数组中。

[yourArray addObject:[NSNumber numberWithInteger:button.tag]];

在你的 .h 文件中定义这个:

@property(nonatomic,strong) NSMutableArray *buttonTapArray;

在你setUpGame这样做:

self.buttonTapArray = [[NSMutableArray alloc] init];

然后在你的buttonPress函数中添加第一个片段

[self.buttonTapArray addObject:[NSNumber numberWithInteger:button.tag]];

要重置订单,只需重新初始化数组

self.buttonTapArray = [[NSMutableArray alloc] init];
于 2013-04-29T15:25:31.580 回答
1

For examine a sequence of presses you can use:

create class variable:

NSMutableArray *_requiredSequence = [@[@"C", @"D", @"E", @"F", @"G", @"A", @"B" ] mutableCopy];

And then:

- (IBAction)buttonPressed: (UIButton*) button {
    NSString *pressedNote = [self noteNameForButtonWithTag:button.tag];
    NSString *requiredNote = _requiredSequence[0];

    if ([pressedNote caseInsensitiveCompare:requiredNote] == NSOrderedSame]) {
        waitinglabel.text = pressedNote;  
        [array removeObjectAtIndex:0];
        if ([array count] == 0) {
            //finish
        }  
    } else {
        //show alert
    }
}

- (NSString *)noteNameForButtonWithTag:(NSUInteger)tag {
    static NSArray *noteNames = @[@"C", @"C#", @"D", @"D#", ....];
    NSUInteger index = tag - 1;
    return scaleNames[index];
}
于 2013-04-29T15:41:43.497 回答
1

我建议你制作NSString你的按钮按下事件。

就像你finalOrder有的是ABCD然后

NSString *order;

1stButtonPress -> order=@"A";
2ndButtonPress -> [order stringByAppendingString:@"B"];
3rdButtonPress -> [order stringByAppendingString:@"C"];
4thButtonPress -> [order stringByAppendingString:@"D"];

不符合这个

if ([order isEqualToString:finalOrder])
    {
        //Your Work
    }
    else{
        //invalid format
    }  

编辑

根据你的问题替换intNSStringie

NSString *finalorder = @12";  
NSString *order = @"1";
于 2013-04-29T17:19:38.647 回答
0

我会使用NSString来存储按下的比例序列。使用 stringByAppendingString 方法添加到序列中,并使用 rangeOfString 来检查按下的序列是否是正确比例顺序的子集

于 2013-04-29T19:40:46.423 回答