how to make button label change randomly

Discussion in 'iOS Programming' started by theprizerevealed, Oct 13, 2016.

  1. theprizerevealed macrumors member

    Joined:
    Feb 26, 2016
    #1
    Can someone tell me if there is a way to make a button label change randomly each time the app program has started or maybe each time a new view controller is segued to when all other aspects of the arrangement of buttons and their functions remains the same? thanks for your input
     
  2. AxoNeuron, Oct 13, 2016
    Last edited: Oct 13, 2016

    AxoNeuron macrumors 65816

    AxoNeuron

    Joined:
    Apr 22, 2012
    Location:
    The Left Coast
    #2
    Yes...but you have to be a bit more specific. Do you mean you want it to be set truly randomly, ie. set to random characters & numbers? Or do you have a list of "possible" values that you want it to randomly pick from?

    Swift
    Code:
    
    let possibilities = ["Test0", "Test1", "Test2", "Test3", "Test4", "Test5"];
    
    self.label.text = possibilities[self.generateRandomNumber(max: possibilities.count)];
    
    func generateRandomNumber(max: Int) -> Int
    {
         return Int(arc4random_uniform(UInt32(max))) + 1;
    }
    
    Objective-C
    Code:
    NSArray *possibilities = @[@"Test0", @"Test1", @"Test2", @"Test3", @"Test4", @"Test5"];
    
    self.label.text = possibilities[[self generateRandomNumber:(int)possibilities.count]];
    
    -(int)generateRandomNumber:(int)max
    {
        return (int)(arc4random_uniform(max) + 1);
    }
    
     
  3. theprizerevealed thread starter macrumors member

    Joined:
    Feb 26, 2016
    #3
    ah, thank you for your kind reply - I seem to have deciphered the matter.
     

Share This Page