开发者

Is it possible to show alert in `clickedButtonAtIndex` method of alertview

In delegate method ofUIalerview I am trying to show another alert after finishing the process on clicking button index. Is it possible to do so? Also I want to call a method on button click of alert. How can I do that?

I am trying in this way. Is is correct?

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{
    if(buttonIndex == 1)
    {
        inappPurchaseViewController  = [[InAppPurchaseViewController alloc] init];
        [inappPurchaseViewController Upgrade:nil];
        [inappPurchaseViewController re开发者_开发技巧lease];

        UIAlertView *purchasedone = [[UIAlertView alloc] initWithTitle:@"Enable Ads" message:@"You can Enable Ads from Settings Option" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [purchasedone show];
        [purchasedone release];
    }  
}


You want to use tags for your UIAlertViews

Assigning tags

UIAlertView *firstAlert = [[UIAlertView alloc] initWithTitle:@"Do something first" message:@"This is the first UIAlertView" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[firstAlert setTag: 0];
[firstAlert show];
[firstAlert release];

Handling those tags

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{
    switch (alertView.tag)
    {
        case 0: /* firstAlert */
        {
            if(buttonIndex == 1)
            {
                inappPurchaseViewController  = [[InAppPurchaseViewController alloc] init];
                [inappPurchaseViewController Upgrade:nil];
                [inappPurchaseViewController release];

                UIAlertView *purchaseDone = [[UIAlertView alloc] initWithTitle:@"Enable Ads" message:@"You can Enable Ads from Settings Option" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [purchaseDone setTag: 1];
                [purchaseDone show];
                [purchaseDone release];
            } 
        }
            break;
        case 1: /* purchaseDone */
        {
            /*  purchaseDone uialertview was triggered, handle it here.   */
        }
            break;
    }
}


 UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"a" message:@"b" delegate:self        cancelButtonTitle:@"OK" otherButtonTitles:@"ca",nil];
[a show];
[a release];


 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

if(buttonIndex == 1)
{
    UIAlertView *purchasedone = [[UIAlertView alloc] initWithTitle:@"Enable Ads" message:@"You can Enable Ads from Settings Option" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    purchasedone.tag = 55;
    [purchasedone show];
    [purchasedone release];

}  

if([alertView tag] == 55 && buttonIndex == 0)
{
    UIAlertView *purchasedone = [[UIAlertView alloc] initWithTitle:@"New Alert" message:@"New Alert Message" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [purchasedone show];
    [purchasedone release];
}

}

write UIAlertViewDelegate in .h file.


put the following line on top of your .m file

`#define PREVIOUS_ALERT 101`
`#define NEW_ALERT 102`

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    //you need to make a condition here for checking the alert else it will go into infinite loom of showing alert after alert
    //you can do it by setting tag to alert something like this
    if(alertView.tag == PREVIOUS_ALERT) //sot the previous alert tag to this where you created it
    {
        if(buttonIndex == 1)
        {

            inappPurchaseViewController  = [[InAppPurchaseViewController alloc] init];
            [inappPurchaseViewController Upgrade:nil];
            [inappPurchaseViewController release];


            UIAlertView *purchasedone = [[UIAlertView alloc] initWithTitle:@"Enable Ads" message:@"You can Enable Ads from Settings Option" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            purchasedone.tag = NEW_ALERT;
            [purchasedone show];
            [purchasedone release];
        }  
    }
    else
    {
        //code for your second alert
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜