关于UIAlertView和UIAlertViewDelegate的简单问题

时间:2011-07-06 23:49:39

标签: iphone delegates uialertview uiactionsheet

我希望这是非常直截了当的。正如您将通过我的代码看到的那样,我只是想通过UIAlertView按钮按下弹回我的根视图。

我没有得到任何编译错误或警告,当我运行应用程序时,在IBAction中调用“RedeemCoupon”方法,并且UIAlertView会弹出它应该的,但它似乎不是“doneRedeeming” “方法被调用 - 我没有看到任何来自NSLog的东西(是的我知道我将buttonIndex设置为0 - 一旦我开始工作,我将修复它)。所以,基本上它不起作用。我点击“取消”按钮,警报就消失了。

顺便说一下,我不确定这是否重要,但是这个“RedeemCouponViewController”视图在堆栈上是4号,并且是在前一个视图中使用presentModalViewController添加的。

如果需要,我愿意采取其他方式 - 欢迎所有建议!

提前致谢!

// RedeemCouponViewController.h

@interface RedeemCouponViewController : UIViewController <UIAlertViewDelegate> {

//  RedeemCouponViewController.m

- (IBAction) redeemYes: (UIButton*) sender {    
    CouponRedeem *redeem = [[CouponDatabase database] couponRedeem:_uniqueId];
    [redeem release];

    UIAlertView *doneRedeeming = [[UIAlertView alloc]initWithTitle:@"Coupon Redeemed!"
                                                           message:@"Thanks for shopping!" 
                                                          delegate:self 
                                                 cancelButtonTitle:@"Back to Main Menu" 
                                                 otherButtonTitles:nil];
    [doneRedeeming show];
    [doneRedeeming release];
  }

-(void) doneRedeeming: (UIAlertView *) doneRedeeming clickedButtonAtIndex: (NSInteger)buttonIndex {
    if (buttonIndex = 0) {
       NSLog(@"doneRedeemed method called");
       [self.navigationController popToRootViewControllerAnimated:YES];
    } else {
       //do nothing
    }
  }

4 个答案:

答案 0 :(得分:2)

你想拥有

if (buttonIndex == 0) {

取代

if (buttonIndex = 0) {

前者检查平等,而后者指定。

另外,你想拥有

– alertView:clickedButtonAtIndex:

你有

的地方
- doneRedeeming:clickedButtonAtIndex:

答案 1 :(得分:1)

您需要使用UIAlertViewDelegate methods

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

-(void) doneRedeeming: (UIAlertView *) doneRedeeming clickedButtonAtIndex: (NSInteger)buttonIndex {}

答案 2 :(得分:0)

使用委托方法-alertView:didDismissWithButtonIndex:侦听取消按钮索引

答案 3 :(得分:0)

@ PengOne的回答是正确的:你的问题是:

if (buttonIndex = 0) {

你说

  

我知道这不正确,但我只是   我想确定声明是这样的   现在是真的...

buttonIndex = 0评估为0,使其等同于

if (0)

无论buttonIndex的值如何,该块中的代码都将永远不会执行。如果您真的想无条件地执行此操作,请将if更改为if( 1 ),或者只取出if。

如果您在调试器中运行此代码,这将是微不足道的。你可能认为你知道你的代码在做什么,但如果你不看它运行,你就不会。

相关问题