我一直在努力在Swift中创建一个UIAlertView,但由于某些原因我无法正确获取该声明,因为我收到此错误:
找不到接受提供的参数的'init'的重载
这是我写的方式:
let button2Alert: UIAlertView = UIAlertView(title: "Title", message: "message",
delegate: self, cancelButtonTitle: "OK", otherButtonTitles: nil)
Run Code Online (Sandbox Code Playgroud)
然后打电话给我我正在使用:
button2Alert.show()
Run Code Online (Sandbox Code Playgroud)
截至目前它正在崩溃,我似乎无法使语法正确.
场景:用户点击视图控制器上的按钮.视图控制器是导航堆栈中最顶层的(显然).tap会调用另一个类上调用的实用程序类方法.在那里发生了一件坏事,我希望在控制返回到视图控制器之前在那里显示警报.
+ (void)myUtilityMethod {
// do stuff
// something bad happened, display an alert.
}
Run Code Online (Sandbox Code Playgroud)
这是可能的UIAlertView(但可能不太合适).
在这种情况下,你如何呈现一个UIAlertController,就在那里myUtilityMethod?
在iOS 8.0中,Apple引入了UIAlertController来取代UIActionSheet.不幸的是,Apple没有添加任何有关如何呈现它的信息.我在hayaGeek的博客上找到了一个关于它的条目,然而,它似乎不适用于iPad.视图完全错位:
放错了地方:

正确:

我使用以下代码在界面上显示它:
let alert = UIAlertController()
// setting buttons
self.presentModalViewController(alert, animated: true)
Run Code Online (Sandbox Code Playgroud)
还有其他方法可以将它添加到iPad吗?或者苹果只是忘了iPad,还是没有实现?
我正在使用新的UIAlertController来显示警报.我有这个代码:
// nil titles break alert interface on iOS 8.0, so we'll be using empty strings
UIAlertController *alert = [UIAlertController alertControllerWithTitle: title == nil ? @"": title message: message preferredStyle: UIAlertControllerStyleAlert];
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle: cancelButtonTitle style: UIAlertActionStyleCancel handler: nil];
[alert addAction: defaultAction];
UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
[rootViewController presentViewController:alert animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)
现在我想更改标题和消息字体,颜色,大小等.什么是最好的方法呢?
编辑: 我应该插入整个代码.我为UIView创建了一个类别,我可以为iOS版本显示正确的警报.
@implementation UIView (AlertCompatibility)
+( void )showSimpleAlertWithTitle:( NSString * )title
message:( NSString * )message
cancelButtonTitle:( NSString * )cancelButtonTitle
{
float iOSVersion = [[UIDevice currentDevice].systemVersion floatValue]; …Run Code Online (Sandbox Code Playgroud) 我尝试了几种方法来使用UIAlertController而不是UIAlertView.我尝试了几种方法,但我无法使警报行动起作用.这是我的代码在IOS 8和IOS 9中正常工作,但是出现了弃用的标志.我尝试了下面的优雅建议,但我无法在此背景下使其发挥作用.我需要提交我的应用程序,这是最后要解决的问题.感谢您的任何进一步建议.我是新手.
#pragma mark - BUTTONS ================================
- (IBAction)showModesAction:(id)sender {
NSLog(@"iapMade: %d", iapMade3);
// IAP MADE ! ===========================================
if (!iapMade3) {
//start game here
gamePlaysCount++;
[[NSUserDefaults standardUserDefaults]setInteger:gamePlaysCount forKey:@"gamePlaysCount"];
NSLog(@"playsCount: %ld", (long)gamePlaysCount);
if (gamePlaysCount >= 4) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Basic"
message: THREE_PLAYS_LIMIT_MESSAGE
delegate:self
cancelButtonTitle:@"Yes, please"
otherButtonTitles:@"No, thanks", nil];
[alert show];
NSString *path = [[NSBundle mainBundle] pathForResource:@"cow" ofType:@"wav"];
_pop =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[_pop play];
[self dismissViewControllerAnimated:true completion:nil];
} else {
if (gamePlaysCount == 1) {
// Create …Run Code Online (Sandbox Code Playgroud) 我有一个tableview,当加载时,每个单元格可能会返回一个NSError,我已经选择在UIAlertController中显示它.问题是如果返回多个错误,我会在控制台中收到此错误.
警告:尝试在MessagesMasterVC上呈现UIAlertController:0x14e64cb00:0x14e53d800已经呈现(null)
理想情况下,我希望在我的UIAlertController扩展方法中处理这个问题.
class func simpleAlertWithMessage(message: String!) -> UIAlertController {
let alertController = UIAlertController(title: nil, message: message, preferredStyle: UIAlertControllerStyle.Alert)
let cancel = UIAlertAction(title: "Ok", style: .Cancel, handler: nil)
alertController.addAction(cancel)
return alertController
}
Run Code Online (Sandbox Code Playgroud)
基于matt的答案,我将扩展名更改为UIViewController扩展,更加清晰,并节省了大量的presentViewController代码.
func showSimpleAlertWithMessage(message: String!) {
let alertController = UIAlertController(title: nil, message: message, preferredStyle: UIAlertControllerStyle.Alert)
let cancel = UIAlertAction(title: "Ok", style: .Cancel, handler: nil)
alertController.addAction(cancel)
if self.presentedViewController == nil {
self.presentViewController(alertController, animated: true, completion: nil)
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用输入发出警报消息,然后从输入中获取值.我发现了很多很好的教程如何制作输入文本字段.但我无法从警报中获得价值.
我正在向UIAlertView用户展示一个,我无法弄清楚如何编写处理程序.这是我的尝试:
let alert = UIAlertController(title: "Title",
message: "Message",
preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Okay",
style: UIAlertActionStyle.Default,
handler: {self in println("Foo")})
Run Code Online (Sandbox Code Playgroud)
我在Xcode中遇到了很多问题.
文件说 convenience init(title title: String!, style style: UIAlertActionStyle, handler handler: ((UIAlertAction!) -> Void)!)
目前整个街区/封闭都有点过头了.任何建议都非常感谢.
我试图UIAlertController用a表示UITextView.当我添加行:
//Add text field
alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in
}
Run Code Online (Sandbox Code Playgroud)
我收到一个Runtime错误:
致命错误:在展开Optional值时意外发现nil
let alertController: UIAlertController = UIAlertController(title: "Find image", message: "Search for image", preferredStyle: .Alert)
//cancel button
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
//cancel code
}
alertController.addAction(cancelAction)
//Create an optional action
let nextAction: UIAlertAction = UIAlertAction(title: "Search", style: .Default) { action -> Void in
let text = (alertController.textFields?.first as! UITextField).text
println("You entered \(text)")
}
alertController.addAction(nextAction)
//Add …Run Code Online (Sandbox Code Playgroud) 在iOS 12.2上。使用UIAlertController的actionSheet Xcode时,遇到此问题的人都会遇到约束错误
相同的代码在iOS 12.1上运行,没有错误
我已经在Xcode 10.2和10.1上测试了此代码
class ViewController: UIViewController {
let Click : UIButton = {
let button = UIButton(type: UIButton.ButtonType.system)
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle("OK", for: .normal)
button.tintColor = UIColor.blue
button.addTarget(self, action: #selector(click(_:)), for: UIControl.Event.touchUpInside)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(Click)
Click.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
Click.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}
@objc func click(_ sender: UIButton) {
let optionMenu = UIAlertController(title: nil, message: "Choose Option", preferredStyle: .actionSheet)
let deleteAction = UIAlertAction(title: "Delete", style: .default)
let saveAction = …Run Code Online (Sandbox Code Playgroud) ios ×9
swift ×4
uialertview ×4
objective-c ×2
cocoa-touch ×1
input ×1
ios8 ×1
ipad ×1
swift5 ×1
uitextfield ×1
xcode ×1