简体   繁体   English

关于 UIAlertView 和 UIAlertViewDelegate 的简单问题

[英]Simple question on UIAlertView and UIAlertViewDelegate

I hope this is pretty straight-forward.我希望这很简单。 As you'll see by my code, I'm simply trying to get a UIAlertView button-press to pop me back to the root view.正如您将在我的代码中看到的那样,我只是试图让 UIAlertView 按钮按下以将我弹回根视图。

I don't get any compile errors or warnings, and when I run the app, the "RedeemCoupon" method is called in the IBAction, and the UIAlertView pops up as it should, but it doesn't seem the "doneRedeeming" method gets called at all - I don't see anything from NSLog (yes I'm aware that I am setting buttonIndex to 0 - once I get this working I'll fix it).我没有收到任何编译错误或警告,当我运行应用程序时,在 IBAction 中调用了“RedeemCoupon”方法,并且 UIAlertView 会按应有的方式弹出,但似乎没有“doneRedeeming”方法得到根本没有调用 - 我从 NSLog 看不到任何东西(是的,我知道我将 buttonIndex 设置为 0 - 一旦我得到这个工作,我会修复它)。 So, basically it doesn't work.所以,基本上是行不通的。 I click the "cancel" button and the alert just goes away.我单击“取消”按钮,警报就消失了。

By the way I'm not sure if this matters, but this "RedeemCouponViewController" view is number 4 on the stack, and it was added by use of presentModalViewController in the previous view.顺便说一句,我不确定这是否重要,但这个“RedeemCouponViewController”视图在堆栈上是第 4 位,并且它是通过在前一个视图中使用 presentModalViewController 添加的。

I'm open to other ways of doing this if needed - all suggestions welcome!如果需要,我愿意接受其他方法 - 欢迎所有建议!

Thanks in advance!提前致谢!

// 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
    }
  }

You want to have你想拥有

if (buttonIndex == 0) {

in place of代替

if (buttonIndex = 0) {

The former checks for equality whereas the latter assigns.前者检查相等,而后者分配。

Also, you want to have另外,你想拥有

– alertView:clickedButtonAtIndex:

where you have你在哪里

- doneRedeeming:clickedButtonAtIndex:

You need to use UIAlertViewDelegate methods :您需要使用UIAlertViewDelegate 方法

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

not不是

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

Use the delegate method -alertView:didDismissWithButtonIndex: to listen for your cancel button index使用委托方法-alertView:didDismissWithButtonIndex:来监听你的取消按钮索引

@PengOne's answer is correct: your problem is this: @PengOne 的回答是正确的:你的问题是这样的:

if (buttonIndex = 0) {

You said你说

I know it's not correct, but I just wanted to be sure the statement was true for now...我知道这不正确,但我只是想暂时确定该陈述是正确的...

But buttonIndex = 0 evaluates to 0 , making it equivalent to但是buttonIndex = 0的计算结果为0 ,使其等效于

if (0)

The code within that block will never execute, regardless of the value of buttonIndex.无论 buttonIndex 的值如何,该块中的代码都不会执行。 If you really want to do it unconditionally, change the if to if( 1 ) , or just take the if out.如果您真的想无条件地这样做,请将 if 更改为if( 1 ) ,或者将 if 去掉。

This would have been trivial to spot if you ran this code in the debugger.如果您在调试器中运行此代码,这将是微不足道的。 You might think you know what your code is doing, but if you don't watch it run, you don't.你可能认为你知道你的代码在做什么,但如果你不看它运行,你就不会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM