编辑:伙计们,你们太棒了,谢谢所有的回复。我决定采用 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