문제

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!

도움이 되었습니까?

해결책

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