在swift的alert中使用哪个标识符代替“present”

时间:2017-11-24 06:31:32

标签: ios swift uialertview presentviewcontroller

当我在当前警报线的swift中使用警报时,我得到了使用未解析标识符的错误。

文件名: CommonFunctions.swift

类名:类CommonFunctions:NSObject

我正在使用此代码

class func gotoSettingScreen()
{
    let alertController = UIAlertController (title: "Title", message: "Go to Settings?", preferredStyle: .alert)

    let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
        guard let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) else {
            return
        }

        if UIApplication.shared.canOpenURL(settingsUrl) {
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
                    print("Settings opened: \(success)") // Prints true
                })
            } else {
                // Fallback on earlier versions
            }
        }
    }
    alertController.addAction(settingsAction)
    let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
    alertController.addAction(cancelAction)


    present(alertController, animated: true, completion: nil)
}

线路错误来了:present(alertController, animated: true, completion: nil)

错误是:enter image description here

感谢您的帮助!和欣赏!

3 个答案:

答案 0 :(得分:1)

present(_:animated:completion:)UIViewController UINavigationController个派生类型(如class)。因此,从某个随机类的let alertController = UIAlertController(title: "title", message: "message", preferredStyle: .alert) //... var rootViewController = UIApplication.shared.keyWindow?.rootViewController if let navigationController = rootViewController as? UINavigationController { rootViewController = navigationController.viewControllers.first } if let tabBarController = rootViewController as? UITabBarController { rootViewController = tabBarController.selectedViewController } rootViewController?.present(alertController, animated: true, completion: nil) (静态)函数调用它当然会失败。

所以你在这里有设计问题。

解决方法从应用中的任何位置呼叫提醒:

module.exports = function (config) {
    config.set({
        basePath: "",
        frameworks: [
            'jasmine',
            'karma-typescript'
        ],
        reporters: [
            "progress",
            "karma-typescript",
            "html"
        ],
        preprocessors: {
            '**/*.ts': [
                'karma-typescript'
            ]
            ,'app/*.*scss': ['scss']
            ,'app/**/*.*scss': ['scss']
        },
        files: [
            {
                pattern: 'test/base.ts'
            },
            {
                pattern: 'app/**/*.ts'
            },
            {
                pattern: 'test/*.spec.ts'
            },
            {
                pattern: "./app/i18n/*.json",
                watched: true,
                served: true,
                included: false
            },
            { pattern: 'app/*.*scss', watched: true,  included: true, served: true },        
            { pattern: 'app/main.ts', watched: false,  included: false, served: false },        
        ],
        karmaTypescriptConfig: {
            exclude: ["broken"],
            tsconfig: './tsconfig.json',
            coverageOptions: {
                instrumentation: true                
            },
            bundlerOptions: {
                entrypoints: /base\.ts|\.spec\.ts$/,
                transforms: [
                    require('karma-typescript-es6-transform')({
                        presets: ['es2015', 'stage-0'],
                        extensions: ['.ts', '.js'],
                        plugins: [
                            ["transform-runtime", {
                                regenerator: true,
                                polyfill: true
                            }]
                        ]
                    })
                ]
            }
        },
        logLevel: config.LOG_INFO,
        browsers: [
            'Chrome'
        ]
    });
};

根据您的UI架构,您可以删除不需要的支票。

建议的代码段取自instance method

答案 1 :(得分:0)

  

你需要一个ViewController的参考,你想要显示它   警报。由于您使用的是自定义类,NSObject:CommonFunctions,   当你想要显示警报时,你需要传入实例   视图控制器或将视图控制器设置为其中一个属性   CommonFunctions类。

示例:

// Basic.Swift 
......

var viewController : UIViewController?

调用显示警报的方法时,将视图控制器属性设置为当前实例,如

CommonFunctions.viewController = self

您必须修改CommonFunctions的getSettingScreen()函数才能使用

viewController.present()

而不是present()

这应该有所帮助。

答案 2 :(得分:0)

如果您要使用非uiviewcontroller发出的警报,则可以通过

之类的uiviewcontroller实例表单方法

NonUicontroller类中的方法

func alertDilog(viewController :UIViewController)
{
   let alertController = UIAlertController(title: title, message: msg, preferredStyle: UIAlertControllerStyle.alert)
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
            (result : UIAlertAction) -> Void in
            print("OK")
   alertController.addAction(okAction)
        vc.present(alertController, animated: true, completion: nil)
    }
        }

要在UiViewcontroller类中使用alertDialog

yourclassName.alertDilog(viewController :self)