UIAlertView title does not display

回眸只為那壹抹淺笑 提交于 2019-12-19 11:56:12

问题


I have created an alert as follows

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

 [alert1 show];

But it does not display the title. How can I fix this?


回答1:


I try in Xcode 5.1.1 in your code is working fine in my Xcode, see the output

and i also try in Xcode 6.0.1 your code is working fine in my Xcode, see the output

if u r using in Xcode 6.0.1 in swift

UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead.

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

another choice

 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)

need more help use this link http://www.appcoda.com/uialertcontroller-swift-closures-enum/




回答2:


Try this for 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:


I have just removed the following method from my ViewController category class and it's working fine!

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



回答4:


From Xcode 6.0 UIAlertView class:

UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead.

On swift ( iOS 8 and OS X 10.10 ), you can do this:

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")
        }



回答5:


You can use following code using UIAlerController for XCode 6 and 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];

Hope it is useful to you..



来源:https://stackoverflow.com/questions/25927676/uialertview-title-does-not-display

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!