Frage

I currently have a button in my game that calls a different ad every time its tapped. Here is the code.

- (IBAction)VideoAds:(id)sender{

int random = arc4random_uniform(2);

switch (random)
{
    case 0:
    {
        VungleSDK* sdk = [VungleSDK sharedSDK];
        [sdk playAd:self];

        NSLog(@"Case 0 Displayed - Vungle");
    }
        break;
    case 1:
        [AdColony playVideoAdForZone:@"APP_ID" withDelegate:nil];
        NSLog(@"Case 1 Displayed - AdColony");

        break;

   }

}

Everything works fine and when I tap the button I get either Case 0 or Case 1 randomly. I want to change that so that get each case in order or for them to alternate back and forth. I do not want random. I know it's probably just a simple 10 second fix, but I've spent over an hour searching Google and Stackoverflow trying to figure out how to do it. Thanks for any help!

War es hilfreich?

Lösung

Try this :

iHoldCaseNo will be golbal variable which hold the iHoldCaseNo case no to be execute.

- (IBAction)VideoAds:(id)sender{

int random = iHoldCaseNo;

switch (random)
{
    case 0:
    {
        VungleSDK* sdk = [VungleSDK sharedSDK];
        [sdk playAd:self];
        iHoldCaseNo=1;
        NSLog(@"Case 0 Displayed - Vungle");
    }
        break;
    case 1:
        [AdColony playVideoAdForZone:@"APP_ID" withDelegate:nil];
        NSLog(@"Case 1 Displayed - AdColony");
        iHoldCaseNo=0;
        break;

   }

}

Or alternate way is to set the sender i.e button tag as 0 or 1 and according to that handle the switch case.

//For setting the tag use.
sender.tag=1 in case 0 and sender.tag=1 in case 1 
//to get random no,
int random = sender.tag;
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top