I have written the program, which chooses a random element from the array.
Once I press the button “Start”, how do I loop that code?
I would like, that once a button “Start” is pressed, a new element from the array is chosen and written in the text field every 5 seconds…. Thanks for the answer.
@implementation MARandom
- (IBAction)Start:(id)sender {
NSArray *tones;
tones = [NSArray arrayWithObjects: @"F#0", @"Gb0", @"G0", @"G#0",@"Ab0",@"A0",@"A#0",@"Bb0",@"B0",
@"C1",@"C#1",@"Db1",@"D1",@"D#1",@"Eb1",@"E1",@"F1",@"F#1",@"Gb1",@"G1",@"G#1",@"Ab1",@"A1",@"A#1",@"Bb1",@"B1",
@"C2",@"C#2",@"Db2",@"D2",@"D#2",@"Eb2",@"E2",@"F2",@"F#2",@"Gb2",@"G2",@"G#2",@"Ab2",@"A2",@"A#2",@"Bb2",@"B2",
@"C3",@"C#3",@"Db3",@"D3",@"D#3",@"Eb3",nil];
i= (arc4random() % 48);
NSString *Tone;
Tone = [tones objectAtIndex: i];
[TextField setStringValue:(NSString *)Tone];
}
Please try this:
and also consider preparing the array at one place rather than the code that will be repeatedly called, perhaps at viewDidLoad.
Note: untested code, but should work.