加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Objective-c类和swift类之间的NSNotificiation

发布时间:2020-12-16 07:11:37 所属栏目:百科 来源:网络整理
导读:我有一个带有 objective-c类和swift类的项目.现在,我需要发布objective-c类的通知: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // Here I present swift view controller CollectionViewController *
我有一个带有 objective-c类和swift类的项目.现在,我需要发布objective-c类的通知:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // Here I present swift view controller
    CollectionViewController *cvc = [[CollectionViewController alloc] initWithNibName:@"CollectionViewController" bundle:nil];
    UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:cvc];
    [self.navigationController presentViewController:controller animated:YES completion:nil];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"BuildingReady" object:self userInfo:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:chosenBuilding,nil] forKeys:[NSArray arrayWithObjects:@"chosenBuilding",nil]]];
}

我正在快速的课堂上听这个通知:

import UIKit

@objc class CollectionViewController : UIViewController,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {

   override func viewDidLoad() {
      super.viewDidLoad()

      NSNotificationCenter.defaultCenter().addObserver(self,selector: "getBuilding:",name: "BuildingReady",object: nil)
   }

   func getBuilding(notification: NSNotification) {
        let userInfo: Dictionary<String,Building!> = notification.userInfo as! Dictionary<String,Building!>

        self.chosenBuilding = userInfo["chosenBuilding"]
   }

问题是,在swift类中,我从不捕获我的通知(func getBuilding从未被调用).
可能有一个对象是客观c类而第二个是快速类的问题吗?

解决方法

尝试这样的事情:

dispatch_async(dispatch_get_main_queue(),^{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"BuildingReady" object:self userInfo:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:choosedBuilding,nil] forKeys:[NSArray arrayWithObjects:@"choosedBuilding",nil]]];
});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读