在另一个文件的主视图中显示警报

时间:2018-06-08 11:38:28

标签: ios swift uialertcontroller

我有这段代码:

MainViewControler:

func errorLoginMessage(txt: String, title: String){
        let alertController = UIAlertController(title: title, message: txt, preferredStyle: .alert)
        alertController.addAction(UIAlertAction(title: "Ok".localized(), style: .cancel, handler: { (action: UIAlertAction!) in
            exit(0)
        }))
        self.present(alertController, animated: true, completion: nil)
    }

和函数AppSystem.swift中的文件:

func startUpdate(){ 
        let dispatchGroup = DispatchGroup()
        dispatchGroup.enter()

        let cms = ServerConnect()
        cms.getJsonProducts(completion: { (data) in
            switch data {
            case .succes(let data):
                self.saveJsonFileToTheDiskProducts(downloadData: data)
            case .error(let error):
                self.errorLoginMessage(txt: "MainView - Error 101: Configuration files can not be created. \(error)", title: "Blad".localized())
                print("")
                break
            }
            dispatchGroup.leave()
        })
    }

我有这个错误:

self.errorLoginMessage(txt: "MainView - Error 101: Configuration files can not be created. \(error)", title: "Blad".localized())

:类型的价值' AppSystem'没有会员' errorLoginMessage'

有谁知道如何修复它?

1 个答案:

答案 0 :(得分:0)

由于提醒不属于您的AppSystem课程,因此您需要将消息传递给MainViewController,以便在出现故障时显示消息。

您可以使用DelegatesNotifiationsCompletion blocks,这是completion block的示例:

func startUpdate(completion:@escaping(Bool)->()){
     // No need of DispatchGroup as pointed by Anbu.Karthik, as with completion block you are making this function work asynchronously only.
    //let dispatchGroup = DispatchGroup()
    //dispatchGroup.enter()

    let cms = ServerConnect()
    cms.getJsonProducts(completion: { (data) in
        switch data {
        case .succes(let data):
            self.saveJsonFileToTheDiskProducts(downloadData: data)
            completion(true)
        case .error(let error):
            completion(false)
            //self.errorLoginMessage(txt: "MainView - Error 101: Configuration files can not be created. \(error)", title: "Blad".localized())
            print("")
            break
        }
        //dispatchGroup.leave()
    })
}

MainViewController中将其称为

startUpdate { (success) in
    if !success {
        //Show alert
        self.errorLoginMessage(txt: "MainView - Error 101: Configuration files can not be created. \(error)", title: "Blad".localized())
    }
}