是否可以在alertview的`clickedButtonAtIndex`方法中显示警报

时间:2011-08-18 05:35:02

标签: iphone objective-c ios4

UIalerview的委托方法中,我试图在完成单击按钮索引的过程后显示另一个警报。有可能这样做吗?此外,我想在按钮点击警报时调用方法。我怎样才能做到这一点?

我正在尝试这种方式。是对的吗?

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{
    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 show];
        [purchasedone release];
    }  
}

3 个答案:

答案 0 :(得分:2)

您想使用UIAlertViews

的标签

分配标签

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];

处理这些标签

- (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;
    }
}

答案 1 :(得分:0)

 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];
}

}

在.h文件中写下UIAlertViewDelegate。

答案 2 :(得分:0)

将以下行放在.m文件的顶部

`#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
    }
}