Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6606543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:24:26+00:00 2026-05-25T19:24:26+00:00

I have written the program, which chooses a random element from the array. Once

  • 0

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];
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:24:26+00:00Added an answer on May 25, 2026 at 7:24 pm

    Please try this:

    - (IBAction)Start:(id)sender {
    [NSTimer scheduledTimerWithTimeInterval:5.0
                                     target:self
                                   selector:@selector(updateTextFieldWithRandomNumber)
                                   userInfo:nil
                                    repeats:YES];
    }
    
    -(void)updateTextFieldWithRandomNumber{
    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];
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a C++ program (which executes from the command line), that works
I have written a program which reads input from csv file and its working
I have written a program that takes the filename from argv[1] and do operations
I have written a program that will etablish a network connection with a remote
I have a console program written in C# that I am using to send
Assume that i have written a program in C++ without using RTTI and run-time
I have written a java program named Automate.java, in which the another java program
I have written the java code below, which executes another java program named Newsworthy_RB.
This is currently my MySQL UPDATE query, which is called from program written in
I have written a program which sends more than 15 queries to Google in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.