标签: uialertcontroller

如何在swift中添加从我的appdelegate类调用的弹出窗口?

我正在为我的ios应用程序开发一个谷歌登录教程,当我们无法登录用户到我的应用程序时,有一部分.

到目前为止,appDelegate.swift中的代码部分如下所示:

guard error == nil && data != nil else {
     // check for fundamental networking error
     print("error=\(error)")

     //lets put popup here that says cant connect to server

     GIDSignIn.sharedInstance().signOut()
     return
}
Run Code Online (Sandbox Code Playgroud)

现在而不是打印错误我想放置警告弹出窗口.我试着在那里写:

  let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
  alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
  self.presentViewController(alert, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

但接着说我接到xcode错误self.presentViewControllervalue of type AppDelegate has no member presentViewController.

在这种情况下如何显示警告弹出窗口?

cocoa-touch uialertview ios swift uialertcontroller

3
推荐指数
2
解决办法
5222
查看次数

在UIAlertController中更改标题颜色

我有两个按钮,但我只想将一个
更改为红色.当我使用下面的功能时,它会全部变为红色.我只想改变一个按钮的颜色.我该怎么做?

alertController.view.tintColor = UIColor.redColor()
Run Code Online (Sandbox Code Playgroud)

objective-c ios swift uialertcontroller

3
推荐指数
3
解决办法
7788
查看次数

带有白色边框的alertController

我创建了一个带有操作表和两个按钮的警报控制器。由于操作表将具有圆形边缘,因此在屏幕上的四个角上会出现白色。我尝试更改警报控制器的背景颜色,但这使操作表成为具有锐边边框而不是圆形边框的矩形。我尝试将视图背景颜色设置为清晰颜色也尝试设置边框半径。这是我的行动表。我希望这些白色边缘不可见。

在此输入图像描述

另外,如何更改取消的背景颜色。

let cancelAction = UIAlertAction(title: "Cancel".localize(), style: .cancel) { _ in
    // this is where I am creating the cancelAction
}
Run Code Online (Sandbox Code Playgroud)

编辑 - 1:添加警报控制器代码

func showActionSheet(_ changeAction: UIAlertAction) {
    let alertController = UIAlertController(title: "", message: "Here is my alert text".localize(), preferredStyle: .actionSheet)

    alertController.view.tintColor = StyleKit.goldenColor
    let attributedString = NSAttributedString(string: alertController.message!, attributes: [
        NSForegroundColorAttributeName : StyleKit.whiteColor
        ])
    alertController.setValue(attributedString, forKey: "attributedMessage")
    if let subview = alertController.view.subviews.first, let alertContentView = subview.subviews.first {
        for innerView in alertContentView.subviews {
            innerView.backgroundColor = StyleKit.popoverDefaultBackgroundColor

        }

    } …
Run Code Online (Sandbox Code Playgroud)

ios swift uialertcontroller

3
推荐指数
1
解决办法
1923
查看次数

当按下按钮时 UIAlertController 被关闭时收到通知

我想在任何 UIAlertController 由于用户点击警报按钮之一而自行关闭(动画完成)之前和之后执行一些操作并呈现一些 UI。

我怎样才能收到用户按下 UIAlertController 中的某个按钮并且它将被解雇然后又被解雇的通知?

在文档中,建议不要子类化 UIAlertController。我仍然尝试过子类化,认为也许它内部会调用func dismiss(animated flag: Bool, completion: (() -> Void)? = nil)自己。类似的self.dismiss(...,不过iOS10上好像不是这样。

我还尝试将“手动”解雇添加到 UIAlertAction 处理程序中:

let alert = UIAlertController.init(...
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: { action in
    alert.dismiss(animated: true, completion: { 
        print("Dismissed")
    })
})
alert.addAction(defaultAction)
Run Code Online (Sandbox Code Playgroud)

但似乎警报在按下按钮后但在调用处理程序之前被解除。无论如何它也不太好用。即使它有效,记住将我的代码添加到每个 UIAlertAction 处理程序中也会有点麻烦。

我将不胜感激任何想法。

uiviewcontroller ios uialertcontroller

3
推荐指数
1
解决办法
2985
查看次数

UIAlertController 无法在 swift 3 的启动视图中工作

我似乎无法在视图启动时弹出警报视图。代码如下。

import UIKit

class StartController: UIViewController
{
    override func viewDidLoad()
    {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor.white;

        startTest();
    }

    func startTest()
    {
        let alerta = UIAlertController(title: "Invalid Test", message: "Testing alert controller", preferredStyle: UIAlertControllerStyle.alert);

        alerta.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil));

        self.present(alerta, animated: true, completion: nil);
    }
}
Run Code Online (Sandbox Code Playgroud)

ios swift uialertcontroller

3
推荐指数
1
解决办法
309
查看次数

测试 UIAlertController 是否已呈现

我有一个协议来允许我的 ViewController 呈现警报。

import UIKit

struct AlertableAction {
    var title: String
    var style: UIAlertAction.Style
    var result: Bool
}

protocol Alertable {
    func presentAlert(title: String?, message: String?, actions: [AlertableAction], completion: ((Bool) -> Void)?)
}

extension Alertable where Self: UIViewController {
    func presentAlert(title: String?, message: String?, actions: [AlertableAction], completion: ((Bool) -> Void)?) {
        let generator = UIImpactFeedbackGenerator(style: .medium)
        generator.impactOccurred()
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
        actions.forEach { action in
            alertController.addAction(UIAlertAction(title: action.title, style: action.style, handler: { _ in completion?(action.result) })) …
Run Code Online (Sandbox Code Playgroud)

xctest swift uialertcontroller

3
推荐指数
1
解决办法
2807
查看次数

使用 SwiftUI 在条件中显示警报

我知道 anAlert可以作为 a 的函数呈现Button,但是 anAlert可以在条件中呈现吗?如:

struct ContentView: View {

    var body: some View {
        Text("Hello World!")
    }
}    

if isValid {

    //present alert
    let alert = UIAlertController(title: "My Title", message: "This 
    is my message.", preferredStyle: UIAlertController.Style.alert)

    alert.addAction(UIAlertAction(title: "OK", style: 
    UIAlertAction.Style.default, handler: nil))

    self.present(alert, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)

有了这个我得到

“ContentView”类型的值没有成员“present”

uikit uialertview swift uialertcontroller swiftui

3
推荐指数
1
解决办法
4772
查看次数

UIAlertController 自 iOS 13 起消失

我有以下功能会弹出一个 UIAlert,允许用户更新他们的触觉反馈设置:

- (void)requestHapticSetting{
    UIWindow *alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    alertWindow.rootViewController = [[UIViewController alloc] init];
    alertWindow.windowLevel = UIWindowLevelAlert + 1;
    [alertWindow makeKeyAndVisible];

    if(isHapticOn){
        hapticMessage = @"Haptic feedback is currently\nturned ON.\nPlease update preference.";
    }
    else {
        hapticMessage = @"Haptic feedback is currently\nturned OFF.\nPlease update preference.";
    }

    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Haptic Setting"
                                                                   message:hapticMessage
                                                            preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction* onAction = [UIAlertAction actionWithTitle:@"TURN ON" style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction * action) {
                                                         [self saveHapticSettingOn];
                                                     }];

    UIAlertAction* offAction = [UIAlertAction actionWithTitle:@"TURN OFF" style:UIAlertActionStyleDefault
                                                      handler:^(UIAlertAction * action) {
                                                          [self …
Run Code Online (Sandbox Code Playgroud)

objective-c uialertview uialertcontroller ios13

3
推荐指数
1
解决办法
4101
查看次数

更改 UIAlertViewController 色调颜色

我正在为我的 swift 应用程序使用谷歌登录。当用户点击登录 API 时,会给出这个UIAlert. 有什么办法可以改变它的色调吗?即“取消”和“继续”?

用户界面警报视图

我什至尝试使用下面的 AppDelegate 代码全局更改它;

UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = Color.brandPurple
Run Code Online (Sandbox Code Playgroud)

这没有效果。

uiview uialertview ios swift uialertcontroller

3
推荐指数
1
解决办法
264
查看次数

显示UIAlertController(actionsheet)iOS8时出现的运行时异常

当我在iOS8 Beta5 + Xcode6中显示UIAlertController(ActionSheet)时,运行时异常即将到来.

此Bug仅在iPad设备中发生.

我在使用UIAlertController时遇到了轰鸣声异常.

*由于未捕获的异常'NSGenericException'终止应用程序,原因:'UIPopoverPresentationController(<_UIAlertControllerActionSheetRegularPresentationController:0x15794370>)应该在演示发生之前设置非零的sourceView或barButtonItem.

我的代码显示ActionSheet如下

     // Cancel Button
      UIAlertAction *actionCancel = [UIAlertAction
                                               actionWithTitle:NSLocalizedString(@"IDS_LABEL_CANCEL", nil)
                                               style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                                                   // cancel
                                                   //action handler
                                                   [self actionHandler:nil withTag:0 withButtonIndex:0];
                                               }];

      // print button
      UIAlertAction *actionPrint = [UIAlertAction
                                                      actionWithTitle:NSLocalizedString(@"IDS_LABEL_PRINT", nil)
                                                      style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

                                                          //action handler
                                                          [self actionHandler:nil withTag:kAttachmentActionSheetTag withButtonIndex:0];         
                                             }];

    // Create action sheet
     UIAlertController *alertController = [UIAlertController
                                                      alertControllerWithTitle:nil message:nil
                                                      preferredStyle:UIAlertControllerStyleActionSheet];

[alertController addAction:actionCancel];
[alertController addAction:actionPrint];

     // show aciton sheet
     [self  presentViewController:alertController animated:YES
                                 completion:nil] …
Run Code Online (Sandbox Code Playgroud)

ios8 uialertcontroller

2
推荐指数
1
解决办法
1144
查看次数