Swift:使用完成处理程序从另一个通知ViewController

M A*_*M A 12 uiviewcontroller ios swift

我的应用程序中有2个ViewControllers.其中一个名为vcA的方法是使用一种方法(此处为viewDidLoad)与我的网络层(我的应用程序中的一个类)进行通信.在完成网络作业(将由完成处理程序推断)后,我想通知类vcB调用一个方法来获取网络层提供的一些数据.请看下面的sudo代码:

class Networking {

   static var PublicValue : SomeKindOfClass? = nil

   static func test(completionHandler : (successful : Bool) -> Void) -> Void {
      //Do some networking in background
      Network.BackgroundNetworking() {
         if result = true {
            PublicValue = SomeValue
            completionHandler(successful : true)
         }
         else {
            completionHandler(successful : false)
         }
    }
}

class vcA : UIViewController {

   override func viewDidLoad() {
      super.viewDidLoad()

      // Do any additional setup after loading the view.
      Networking.test(completionHandler : { (successful) in
         if successful == true {
            //Here I want to notify class vcB to call printPublicValue method
         }
      })
   }
}

class vcB : UIViewController {

  func printPublicValue() {

      print(Networking.PublicValue)
   }
}
Run Code Online (Sandbox Code Playgroud)

Jon*_*ing 16

我同意@ Paulw11你应该考虑使用NSNotifications.它们易于设置和使用,并且在这种情况下可以很好地工作.为此,在您的一个视图控制器中输入以下代码:

NSNotificationCenter.defaultCenter().postNotificationName("nameOfNotification", object: nil)
Run Code Online (Sandbox Code Playgroud)

在您的第二个视图控制器(将接收通知的那个)中放置:

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(NameOfViewController.nameOfFunction(_:)), name: "nameOfNotification", object: nil)
Run Code Online (Sandbox Code Playgroud)

然后你可以创建一个这样的函数:

func nameOfFunction(notif: NSNotification) {
      //Insert code here
}
Run Code Online (Sandbox Code Playgroud)

如果你想深入了解,这里有一个很棒的教程:

https://www.andrewcbancroft.com/2014/10/08/fundamentals-of-nsnotificationcenter-in-swift/

编辑:Swift 3实现此.

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "nameOfNotification"), object: nil)
Run Code Online (Sandbox Code Playgroud)

在您的第二个视图控制器(将接收通知的那个)中放置:

NotificationCenter.default.addObserver(self, selector: #selector(self.nameOfFunction), name: NSNotification.Name(rawValue: "nameOfNotification"), object: nil)
Run Code Online (Sandbox Code Playgroud)

然后你可以创建一个这样的函数:

func nameOfFunction(notif: NSNotification) {
      //Insert code here
}
Run Code Online (Sandbox Code Playgroud)

Swift 4实现了这一点.

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "nameOfNotification"), object: nil)
Run Code Online (Sandbox Code Playgroud)

在您的第二个视图控制器(将接收通知的那个)中放置:

NotificationCenter.default.addObserver(self, selector: #selector(self.nameOfFunction), name: NSNotification.Name(rawValue: "nameOfNotification"), object: nil)
Run Code Online (Sandbox Code Playgroud)

然后你可以创建一个这样的函数:

@ objc func nameOfFunction(notif: NSNotification) {
      //Insert code here
}
Run Code Online (Sandbox Code Playgroud)

  • 你永远不应该直接调用视图控制器的`viewDidLoad`.如果你有一个B实例的引用,那么你可以将对象设置为观察者(`addObserver`的第一个参数),但通常一个类应该管理自己的观察.既然你说要调用`viewDidLoad`,实际上你应该在检索数据时触发从A到B(或存在B)的segue,在这种情况下你不需要NSNotifications (4认同)