我有一个带有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]]];
}
我正在swift课程中听到这个通知:
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类而第二个是快速类的问题吗?
答案 0 :(得分:1)
尝试这样的事情:
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"BuildingReady" object:self userInfo:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:choosedBuilding, nil] forKeys:[NSArray arrayWithObjects:@"choosedBuilding", nil]]];
});