Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

theprizerevealed

macrumors regular
Original poster
Feb 26, 2016
183
12
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
 
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);
}
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.