iOS React Native:从ViewDidAppear向View Component发送事件

时间:2017-05-08 23:38:37

标签: react-native-ios

我有一个反应视图作为本机View Controller的主视图。我希望我的本机View Controller在我的反应组件NativeEventEmitter正在侦听的ViewDidAppear之后调度一个事件。

componentDidMount(){
    const eventManager = new NativeEventEmitter(ReactMainManager);

    this._subscription = eventManager.addListener(
       'ViewDidLoad', 
        (info) => {
           //.. do something 
        }
    );
    ...

InsideEventEmitter

@objc func viewWasHit(_ callback: RCTResponseSenderBlock){
  self.sendEvent(withName: "ViewDidLoad", body: [])
}

ReactViewController内部

class subClassReactViewController: ReactViewController{
   override func viewDidAppear(_ animated: Bool){
     if let reactView = reactRootView as? RCTRootView{
        //How do I fire viewHasHit from here? 
     }
  }
}

1 个答案:

答案 0 :(得分:0)

由于缺乏更好的解决方案,我最终使用Notification Center。

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    NotificationCenter.default.post(name: NSNotification.Name("InfoViewAppeared"), object: nil)
}

在RCTEventEmitter中

class ReactAccountInfoManager:RCTEventEmitter {

override init() {
    super.init()
    NotificationCenter.default.addObserver(self, selector: #selector(ReactAccountInfoManager.viewAppeared), name: NSNotification.Name(rawValue: "InfoViewAppeared"), object: nil)
}

deinit {
    NotificationCenter.default.removeObserver(self) 
}



@objc func viewAppeared(){

   self.sendEvent(withName: "ViewAppeared", body: headerDictionary)

}
相关问题