【发布时间】:2016-04-28 09:07:54
【问题描述】:
所以我在 swift 和 Xcode 中构建了我的单视图应用程序。我所有的代码都在一个文件中,主要的 ViewController.swift 以便随着代码变得越来越大而使事情变得更容易我已经开始将这些方法移动到一个单独的 swift 文件中以使事情井井有条 - ClipManager.swift。
我有 3 个方法,它们都使用调用 UIAlertController 的 notifyUser 方法。
因此,我已将此方法移至同一文件 ClipManager.swift,但现在我的警报未显示何时在当前 ViewController 中调用这些方法 - ViewController.swift,因为这些方法位于单独的文件中。
首先是我使用 UIAlert 的方法
////Located in ClipManager.swift
class ClipManager: UIViewController {
func notifyUser(title: String, message: String) -> Void
{
let alert = UIAlertController(title: title,
message: message,
preferredStyle: UIAlertControllerStyle.Alert)
let cancelAction = UIAlertAction(title: "OK",
style: .Cancel, handler: nil)
alert.addAction(cancelAction)
self.presentViewController(alert, animated: true,
completion: nil)
}}
ViewController.swift对我的方法的调用
class ViewController: UIViewController {
///// In ViewController.swift (where I want the Alert to show)
let SavedRecord = MyClipManager.SaveMethod(publicDatabase!, myRecord: record)
}
SaveMethod 的代码位于ClipManager.swift
func SaveMethod(publicDatabase: CKDatabase, myRecord:CKRecord ) -> CKRecord {
publicDatabase.saveRecord(myRecord, completionHandler:
({returnRecord, error in
if let err = error {
//// **** Notify called here *****
self.notifyUser("Save Error", message:
err.localizedDescription)
} else {
dispatch_async(dispatch_get_main_queue()) {
self.notifyUser("Success",
message: "Record saved successfully")
}
}
}))
return myRecord
}
我猜我的警报没有显示,因为它们实际上是在ClipManager.swift 上触发的,而ClipManager.swift 不在视图中。
我在这里有什么选择,将NotifyUser 移回ViewController.swift 并在ClipManager.swift 中创建它的对象以在我位于那里的方法中调用它?
或者有没有办法将这些警报传递给显示的视图?
【问题讨论】:
标签: ios swift methods viewcontroller uialertcontroller