因此,我已經在swift和Xcode中構建了我的單一視圖應用程序。我所有的代碼都在一個文件中,主代碼爲ViewController.swift
,使代碼變得更容易。我已經開始將這些方法移動到一個單獨的swift文件中,以便保持組織結構 - ClipManager.swift
。UIAlertController未顯示何時移動到另一個swift文件
我有3種方法都使用notifyUser
方法,它調用UIAlertController
。
所以我提出這個方法爲同一個文件,ClipManager.swift
但現在我的警報沒有顯示時,這些方法稱爲當前視圖控制器 - 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)
}
對位於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
上觸發的,而這是不可見的。
我在這裏有什麼選擇,將NotifyUser
移回ViewController.swift
,並在ClipManager.swift
中創建此對象並在此處調用它,以便在位於那裏的方法中調用它?
或者有沒有辦法將這些警報傳遞給顯示的視圖?
非常感謝!這工作完美... 你能向我解釋這是如何改變,我明白,rootviewcontroller是選擇第一個視圖控制器? 爲什麼它變成NSObject? – Malorrr
爲NSObject它只是告訴你,你可以使用一個簡單的Swift類的通知管理。 rootView控制器是根視圖包含你的內容 –
'NSObject'部分與解決方案無關,Swift中的所有東西都從'NSObject'繼承,在你的類文件中顯式繼承它並且不包含它。 – pbush25