2017-06-15 60 views
0

其實我是新來的,我在這裏卡住了,任何人都可以解決這個問題。 actOnSpecialNotification Func鍵不ViewController.swiftNsNotificationCenter無法正常工作

呼籲fireNotification

在ViewController.swift

func fireNotification() -> Void { 
NotificationCenter.default.addObserver(self, selector: 
#selector(vikas.updateNotificationSentLabel), name: 
NSNotification.Name(rawValue: mySpecialNotificationKey), object: nil) 
} 
func updateNotificationSentLabel() { 
    print("sent") 
} 

在SecondVC.swift

override func viewDidLoad() { 
    super.viewDidLoad() 

NotificationCenter.default.addObserver(self, selector: 
#selector(ViewController.actOnSpecialNotification), name: 
NSNotification.Name(rawValue: mySpecialNotificationKey), object: nil) 
} 
func actOnSpecialNotification() { 
    print("listen") 
} 
+0

你'fireNotification()'不火任何上述方法的通知,只有一個觀察者。 – Moritz

回答

1

首先添加觀察到您的FirstViewConroller。

FirstViewConroller

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YourClassName.methodOfReceivedNotification(_:)), name:」test」, object: nil) 

現在,添加在相同的ViewController一旦通知將被解僱,這將被稱爲相關的選擇方法。

func methodOfReceivedNotification(notification: Notification){ 
     //Take Action on Notification 
} 

現在,你可以使用火線以下,這將調用駐留在FirstViewController

SecondViewController

NSNotificationCenter.defaultCenter().postNotificationName(「test」, object: nil) 
+0

如果我們從firstviewController向SecondViewController發出通知而不繼續第一次第二次查看 –