【问题标题】:UIAlertView title does not displayUIAlertView 标题不显示
【发布时间】:2014-09-19 06:29:38
【问题描述】:

我已创建如下警报

UIAlertView *alert1 = [[UIAlertView alloc]
                           initWithTitle:@"Title"
                           message:@"message"
                           delegate:self
                           cancelButtonTitle:@"Cancel"
                           otherButtonTitles:nil];

 [alert1 show];

但它不显示标题。我怎样才能解决这个问题?

【问题讨论】:

  • 你在 iOS8 中尝试过吗?如果是这样,请使用 UIAlertController。
  • 你在 Xcode6 或更低版本中使用
  • 这段代码在 iOS8 中运行,我已经检查过了
  • 是的.. Xcode 6 和 iOS8
  • @dheeru 所以在我的情况下标题不显示.. :(

标签: ios objective-c iphone uialertview


【解决方案1】:

我在你的代码中尝试Xcode 5.1.1 在我的 Xcode 中运行良好,查看输出

我也尝试在Xcode 6.0.1 你的代码在我的 Xcode 中运行良好,查看输出

如果你在Xcode 6.0.1 中使用swift

UIAlertView 已弃用。将 UIAlertController 与 preferredStyle 一起使用 UIAlertControllerStyleAlert 代替。

UIAlertController * alert1 = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* aaction = [UIAlertAction actionWithTitle:@"okay" style:UIAlertActionStyleDefault
                                                  handler:^(UIAlertAction * action) {
                                                      [alert1 dismissViewControllerAnimated:YES completion:nil];
                                                  }];
[alert1 addAction:aaction];
[self presentViewController:alert1 animated:YES completion:nil];

另一种选择

 let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert)

    let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
    alertController.addAction(defaultAction)

    presentViewController(alertController, animated: true, completion: nil)

需要更多帮助,请使用此链接http://www.appcoda.com/uialertcontroller-swift-closures-enum/

【讨论】:

  • 试试这个效果很好,如果你需要任何帮助,我一定会帮助你
  • 我还是一样的状态..你能帮帮我吗
  • 你试试 xcode 6 - 目标 - C 或 swift 兄弟,你遇到了什么错误
  • 使用 Xcode 6 和 Objective C .. 问题是标题不显示并且消息像标题一样以粗体显示
  • 对不起,亲爱的..这是不可能的
【解决方案2】:

在 iOS 8 上试试这个

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"AlertView" message:@"I am an AlertView" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                      handler:^(UIAlertAction * action) {
                                                          [alert dismissViewControllerAnimated:YES completion:nil];
                                                      }];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];

【讨论】:

    【解决方案3】:

    我刚刚从我的 ViewController 类别类中删除了以下方法,它工作正常!

    - (void)setTitle:(NSString *)title
    {
       // My Code
    }
    

    【讨论】:

    • 我为此苦苦挣扎了很长时间,几乎放弃了,直到我找到了这个。谢谢!我认为您应该接受自己的答案,因为所有其他人都只是向您展示如何创建警报,但您发布的代码确实会导致问题。
    【解决方案4】:

    来自 Xcode 6.0 UIAlertView 类:

    UIAlertView 已弃用。将 UIAlertController 与首选样式一起使用 UIAlertControllerStyleAlert 代替。

    在 swift(iOS 8 和 OS X 10.10)上,您可以这样做:

    var alert = UIAlertController(title: "Alert Title", message: "Alert Message", preferredStyle: UIAlertControllerStyle.Alert)
            alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Cancel, handler:handleCancel))
            alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler:{ (ACTION :UIAlertAction!)in
                    println("User click Ok button")
                }))
            self.presentViewController(alert, animated: true, completion: nil)
    
    func handleCancel(alertView: UIAlertAction!)
            {
                println("User click cancel button")
            }
    

    【讨论】:

    • 他们没有要求 swift 他们要求 Objective-c -1
    • 我说这个方法在 Xcode 6 上被弃用了,并发布了一个 Swift 的例子,他可以简单地搜索 Objective-c 的 UIAlertController 语法,或者这对你来说似乎太难了?
    • 如果他们想要它在 swift 中,他们会在 swift 中要求它,但他们已经要求objective-c,所以这会使你的答案不正确。另外请不要在您的 cmets 末尾添加侮辱性 cmets,您提供错误答案的人大多数人只会投反对票而不提供理由,至少我给了您一个理由,以便您可以纠正。现在,由于您的粗鲁和侮辱,我的反对票将继续存在。
    • 感谢您在 Swift 中提供解决方案。虽然它没有直接回答 Objective-C 中提出的问题,但它是一个奖励。
    【解决方案5】:

    您可以使用 UIAlerController 为 XCode 6 和 IOS 8 使用以下代码

    UIAlertController * alert=   [UIAlertController
                                     alertControllerWithTitle:@"Title"
                                     message:@"Your Message"
                                     preferredStyle:UIAlertControllerStyleAlert];
    
       UIAlertAction* ok = [UIAlertAction
                            actionWithTitle:@"OK"
                            style:UIAlertActionStyleDefault
                            handler:^(UIAlertAction * action)
                            {
                                [alert dismissViewControllerAnimated:YES completion:nil];
    
                            }];
       UIAlertAction* cancel = [UIAlertAction
                                actionWithTitle:@"Cancel"
                               style:UIAlertActionStyleDefault
                               handler:^(UIAlertAction * action)
                               {
                                   [alert dismissViewControllerAnimated:YES completion:nil];
    
                               }];
    
       [alert addAction:ok];
       [alert addAction:cancel];
    
       [self presentViewController:alert animated:YES completion:nil];
    

    希望对你有用..

    【讨论】:

    • 对不起,但仍然没有显示标题:(
    • 我看不出上面的答案不起作用的任何原因。 @Myaaoonn 你能提供更多你的代码吗?
    • @Myaaoonn 你试过复制粘贴上面提供的答案吗?从技术上讲,应该这样做。
    • @Teffi 当然,我知道这是我们的主要习惯.. 但严重没有效果.. 我不知道是什么问题,是我的 Xcode 问题吗..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多