UIAlertView标题不显示

时间:2014-09-19 06:29:39

标签: ios objective-c iphone uialertview

我已创建如下警告

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

 [alert1 show];

enter image description here

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

5 个答案:

答案 0 :(得分:2)

我在代码中Xcode 5.1.1尝试在我的Xcode中工作正常,请参阅输出

enter image description here

我也尝试Xcode 6.0.1你的代码在我的Xcode中工作正常,看输出

enter image description here

如果您使用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/

答案 1 :(得分:1)

试用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];

答案 2 :(得分:1)

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

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

答案 3 :(得分:0)

来自Xcode 6.0 UIAlertView类:

  

UIAlertView已弃用。将UIAlertController与preferredStyle一起使用   取而代之的是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")
        }

答案 4 :(得分:0)

您可以使用UIAlerController for 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];

希望它对你有用..