2016-01-21 68 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) 
相关问题