Question

I'm just palying around with Xcode (I'd like to start making iPhone apps) and I was wondering how you would make this progress HUD randomly show strings? Currently:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"Give me a sec...";
}

So I'd like it to randomly choose from things like, 'Give me a sec', 'Hold on', etc. Just something I'd like to learn, thanks for any help!

I tried searching around but it's very hard to think of appropiate search terms!

Was it helpful?

Solution

Put all your strings in an array, get a random number between 0 and size of array (minus 1), and use that to retrieve the msg. Put in method to make it easy. Define the msg array outside though, for better code.

Something similar to this:

-(NSString*) getRandomMsg {
    NSArray *arr = @[@"Give me a sec", @"Hold on"];
    int randNum = arc4random_uniform([arr count]);   // or arc4random, possibly rand % max
    NSString *msg = arr[randNum];
    return msg;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top