问题
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