2016-01-21 35 views
1

因此,我已經在swift和Xcode中構建了我的單一視圖應用程序。我所有的代碼都在一個文件中,主代碼爲ViewController.swift,使代碼變得更容易。我已經開始將這些方法移動到一個單獨的swift文件中,以便保持組織結構 - ClipManager.swiftUIAlertController未顯示何時移動到另一個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中創建此對象並在此處調用它,以便在位於那裏的方法中調用它?

或者有沒有辦法將這些警報傳遞給顯示的視圖?

回答

2

嗨,這裏,我將如何在你的地方做。

對於我的ClipManager類,它將從NSObject擴展。無論從UIView的控制器

這裏需要多類應該看起來像

class ClipManager: NSObject { 
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) 
    UIApplication.sharedApplication().keyWindow?.rootViewController!.presentViewController(alert, animated: true, 
     completion: nil) 
} 
} 

呈現我使用的應用程序

rootViewController沒有改變你的ViewController類,使alertView。

讓我知道它是否適合你:)

+0

非常感謝!這工作完美... 你能向我解釋這是如何改變,我明白,rootviewcontroller是選擇第一個視圖控制器? 爲什麼它變成NSObject? – Malorrr

+0

爲NSObject它只是告訴你,你可以使用一個簡單的Swift類的通知管理。 rootView控制器是根視圖包含你的內容 –

+0

'NSObject'部分與解決方案無關,Swift中的所有東西都從'NSObject'繼承,在你的類文件中顯式繼承它並且不包含它。 – pbush25

1

我以前做事情的BEN馬蘇德·馬哈茂德的方式,但在時間,跑了一個小錯誤: 「試圖提出有關誰的觀點是不窗口層次結構!「

爲了解決這個問題,我修改了一下。我將視圖控制器與調用notifyUser一起傳遞。然後,它會從我所在的任何風險投資商處展示。

func notifyUser(title: String, message: String, fromController: UIViewController) -> Void{ 
let alert = UIAlertController(title: title, 
    message: message, 
    preferredStyle: UIAlertControllerStyle.Alert) 

let cancelAction = UIAlertAction(title: "OK", 
    style: .Cancel, handler: nil) 

alert.addAction(cancelAction) 
fromController.presentViewController(alert, animated: true, 
    completion: nil) 
} 

,那麼我會叫它:

Clipmanager.notifyUser("title", message: "message", fromController: self) 

得到了一些幫助,這從Sulthan此鏈接斯坦AlertController is not in the window hierarchy

0

更新和BEN馬蘇德對斯威夫特3答案:

class ClipManager: NSObject { 

func notifyUser(title: String, message: String, fromController: UIViewController) -> Void 
{ 
    let alert = UIAlertController(title: title, 
            message: message, 
            preferredStyle: UIAlertControllerStyle.alert) 

    let cancelAction = UIAlertAction(title: "OK", 
            style: .cancel, handler: nil) 

    alert.addAction(cancelAction) 
    fromController.present(alert, animated: true, completion: nil) 
}} 

並稱之爲:

let clipMGR = ClipManager() 

clipMGR.notifyUser(title: "Title", message: "Message", fromController: self) 
相關問題