问题
I have one obj c class, swift file. And here is my code :
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
#include "InterfaceManagerInstance.h"
void IOSInterfaceManager::testMethod() {}
void IOSInterfaceManager::initialize(){
}
std::string IOSInterfaceManager::getColorPrimary(){
return "";
}
void IOSInterfaceManager::onOver(int nameID,int fameid, int nickNameID){
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"openBoard" object:nil userInfo:userInfo];
NSLog(@"Finished!");
});
}
userInfo:userInfo
is my NSDictionary.
.h file code :
class IOSInterfaceManager : public InterfaceManager
{
public:
void onOver(int nameID,int fameid, int nickNameID);
};
Now in my swift file :
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(self.openBoard(notification:)), name: Notification.Name("openBoard"), object: nil)
}
@objc func openBoard(notification: Notification) {
}
Now in my obj c class NSLog(@"Finished!");
this is getting print in my console. But openBoard
is not printing. Not sure what that the issue here. Any help would be useful.
Thanks in advance !
Update :
When i add breakpoint in my NSNotificationCenter
i am getting this warning :
warning: could not execute support code to read Objective-C class data in the process. This may reduce the quality of type information available.
回答1:
Looking at your sample project, the main problem is that you never instantiate the Swift ViewController
class. So, the NotificationCenter.default.addObserver
code never runs, and the object doesn't exist to get the notification anyway.
In your testLogin.m
class, you need to create a ViewController
property, and instantiate the class. Make it a property and not a local variable so it doesn't go out of scope.
You also need to make a couple changes to your Swift class. Presumably, you intend to load this as a UIViewController
, so you could leave the .addObserver
line in viewDidLoad()
, but... if you haven't yet done anything to actually load the view, that won't be called either. Better to implement the init
methods and do it there.
In your sample project, replace ViewController.swift
with:
//
// ViewController.swift
// notificationCenter
//
// Created by SATHISH on 3/1/19.
// Copyright © 2019 sathish. All rights reserved.
//
import UIKit
@objc class ViewController: UIViewController {
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
print("Swift ViewController class init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) was called.")
setupNotif()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
print("Swift ViewController class init?(coder aDecoder) was called.")
setupNotif()
}
init() {
super.init(nibName: nil, bundle: nil)
print("Swift ViewController class init() was called.")
setupNotif()
}
func setupNotif() -> Void {
NotificationCenter.default.addObserver(self, selector: #selector(self.openScoreBoard(notification:)), name: Notification.Name("openScoreBoard"), object: nil)
}
@objc func openScoreBoard(notification: Notification) {
print("Swift openScoreBoard(notification: Notification) was triggered!")
}
}
and replace testLogin.m with this:
//
// testLogin.m
// notificationCenter
//
// Created by SATHISH on 3/1/19.
// Copyright © 2019 sathish. All rights reserved.
//
#import "testLogin.h"
#import "notificationCenter-Swift.h"
@interface testLogin ()
@property (strong, nonatomic) ViewController *swiftVC;
@end
@implementation testLogin
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// instantiate an instance of the Swift ViewController
_swiftVC = [ViewController new];
}
- (IBAction)clickedAction:(id)sender {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSDictionary* userInfo = @{@"gameScore": @"123", @"gameID": @"123", @"gameSkillID": @"123"};
[[NSNotificationCenter defaultCenter] postNotificationName:@"openScoreBoard" object:nil userInfo:userInfo];
NSLog(@"Finished!");
});
}
@end
There are only a few changes -- if you compare your originals with these, the diffs will be obvious.
来源:https://stackoverflow.com/questions/54930376/notificationcenter-is-not-getting-trigger-in-my-obj-c-class-utills-class-or-ob