Objective-c类和swift类之间的NSNotificiation

Objective-c类和swift类之间的NSNotificiation,第1张

概述我有一个带有 objective-c类和swift类的项目.现在,我需要发布objective-c类的通知: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // Here I present swift view controller Co 我有一个带有 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]]];});
总结

以上是内存溢出为你收集整理的Objective-c类和swift类之间的NSNotificiation全部内容,希望文章能够帮你解决Objective-c类和swift类之间的NSNotificiation所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1227616.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存