ios - UIAlertView alertViewShouldEnableFirstOtherButton : disables wrong button on iOS7

标签 ios ios7 ios6 uialertview

我目前正在开发一款需要支持 iOS6 和 iOS7 的应用。

我正在创建这样的警报:

self.newCategoryAlertView = [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"MessageTitleNewCategory", nil)
                                                    message:NSLocalizedString(@"MessageTextNewCategory", nil)
                                                   delegate:self
                                          cancelButtonTitle:nil
                                          otherButtonTitles:NSLocalizedString(@"ButtonOK", nil), NSLocalizedString(@"ButtonCancel", nil), nil] autorelease];

self.newCategoryAlertView.cancelButtonIndex = 1;
self.newCategoryAlertView.tag = alertViewTypeNewCategory;
self.newCategoryAlertView.alertViewStyle = UIAlertViewStylePlainTextInput;
[self.newCategoryAlertView textFieldAtIndex:0].delegate = self;
[self.newCategoryAlertView textFieldAtIndex:0].autocapitalizationType = UITextAutocapitalizationTypeSentences;
[[self.newCategoryAlertView textFieldAtIndex:0] setReturnKeyType:UIReturnKeyDone];
[[self.newCategoryAlertView textFieldAtIndex:0] setKeyboardAppearance:UIKeyboardAppearanceDefault];
[self.newCategoryAlertView textFieldAtIndex:0].enablesReturnKeyAutomatically = YES;

[self.newCategoryAlertView show];

在委托(delegate)中我实现了以下协议(protocol)方法

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
    if(alertView.tag == alertViewTypeNewCategory)
    {
        UITextField *textField = [alertView textFieldAtIndex:0];
        if (!textField.text || [textField.text isEqualToString:@""])
        {
            return NO;
        } else {
            return YES;
        }
    } else {
        return YES;
    }
}

我的问题是在 iOS6 上运行时左键被禁用(正如预期的那样),但是在 iOS7 上运行时右键被禁用。

我检查了委托(delegate)方法中的 cancelButtonIndex 和 firstOtherButtonIndex 的值,它们在 iOS7 和 iOS6 中是相同的。

有什么提示我做错了吗?或者解决此问题的解决方法?

最佳答案

似乎 iOS 7 已经根据索引更改了按钮的排列顺序。 我已经尝试了您的代码并添加了几个按钮来检查排列顺序。

[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"MessageTitleNewCategory", nil)
                                                        message:NSLocalizedString(@"MessageTextNewCategory", nil)
                                                       delegate:self
                                              cancelButtonTitle:nil
                                              otherButtonTitles:NSLocalizedString(@"ButtonOK", nil), NSLocalizedString(@"ButtonCancel", nil),NSLocalizedString(@"Third Button", nil),NSLocalizedString(@"Fourth Button", nil),NSLocalizedString(@"Fifth Button", nil), nil];

对于 iOS 7 中的 UIAlertView

enter image description here

第一个按钮“ButtonOK”位于顶部,第二个按钮位于最后一个位置,其余按钮按升序排列,与以前的 iOS 版本相同。

因此,您可以使用 [[UIDevice currentDevice] systemVersion] 检查 iOS 版本并执行

   if (iOS 7) {
     self.newCategoryAlertView = [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"MessageTitleNewCategory", nil)
                                                message:NSLocalizedString(@"MessageTextNewCategory", nil)
                                               delegate:self
                                      cancelButtonTitle:nil
                                      otherButtonTitles:NSLocalizedString(@"ButtonCancel", nil),NSLocalizedString(@"ButtonOK", nil), , nil] autorelease];
  }else{
      self.newCategoryAlertView = [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"MessageTitleNewCategory", nil)
                                                message:NSLocalizedString(@"MessageTextNewCategory", nil)
                                               delegate:self
                                      cancelButtonTitle:nil
                                      otherButtonTitles:NSLocalizedString(@"ButtonOK", nil), NSLocalizedString(@"ButtonCancel", nil), nil] autorelease];
  }

关于ios - UIAlertView alertViewShouldEnableFirstOtherButton : disables wrong button on iOS7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19488495/

相关文章:

iOS 6 UISegmentedControl 与 iOS 7 设计

ios - 为 mac 催化剂设置不同的值以键入 info.plist 文件

ios - 在 SKScene 上运行 SKTransition 会破坏原始 SKScene 吗?

ios - 根据屏幕大小调整按钮大小

ios - UIOffsetMake() 是使用传统的左上角原点 (0,0) 还是左下角原点?

iphone - 在 iOS 7 中,为什么 View 高度与 iOS 6 中的相同 View 不同

ios - Facebook iOS SDK v 3.1.1 遇到问题

ios - iOS 6 上的原生 Facebook 分享

ios - 如何以编程方式在 iOS 中播放 .flv 扩展文件

ios - 如何在 AppDelegate 中使用 NSUserDefaults