문제

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!

도움이 되었습니까?

해결책

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;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top