【问题标题】:Simple question on UIAlertView and UIAlertViewDelegate关于 UIAlertView 和 UIAlertViewDelegate 的简单问题
【发布时间】:2011-07-06 23:49:38
【问题描述】:

我希望这很简单。正如您将在我的代码中看到的那样,我只是试图让 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
    }
  }

【问题讨论】:

    标签: iphone delegates uialertview uiactionsheet


    【解决方案1】:

    你想拥有

    if (buttonIndex == 0) {
    

    代替

    if (buttonIndex = 0) {
    

    前者检查相等,而后者分配。

    还有,你想拥有

    – alertView:clickedButtonAtIndex:
    

    你在哪里

    - doneRedeeming:clickedButtonAtIndex:
    

    【讨论】:

    • 是的,谢谢 - 我知道这不正确,但我只是想暂时确定该声明是否正确,以防“取消”由于某种原因不是“0”。不过,感谢您的提示!
    • 附带说明,将比较写为(0 == buttonIndex) 通常更安全,这样如果您不小心分配而不是比较并且避免了潜在的逻辑错误,程序将不会编译。跨度>
    【解决方案2】:

    你需要使用UIAlertViewDelegatemethods:

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

    不是

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

    【讨论】:

    • 试过你的改变(实际上我最初是这样的,但它没有工作:-(
    【解决方案3】:

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

    【讨论】:

    • 试过这个:'-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex: (NSInteger)buttonIndex { if (buttonIndex = 0) { NSLog(@"doneRedeemed method called"); [self.navigationController popToRootViewControllerAnimated:YES]; } else { //什么都不做 } }' 还是不行
    • 应该if (buttonIndex = 0)if (buttonIndex == 0)
    • 是的,你是对的,它应该是 - 我只是想消除它作为一个可能的错误,所以我现在只是手动设置它。
    • 那么,如果您的委托方法没有被调用,那么您的委托设置不正确。检查您设置委托的位置。
    • 好的,我唯一做的就是在头文件中包含“”。还有什么我应该做的吗?
    【解决方案4】:

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

    if (buttonIndex = 0) {
    

    你说

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

    buttonIndex = 0 的计算结果为0,使其等价于

    if (0)
    

    无论 buttonIndex 的值如何,该块中的代码都不会执行。如果你真的想无条件地做,把if改成if( 1 ),或者直接把if去掉。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-03
      • 1970-01-01
      • 2011-09-21
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多