Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从objectivec发送通知并以swift接收_Ios_Objective C_Swift - Fatal编程技术网

Ios 从objectivec发送通知并以swift接收

Ios 从objectivec发送通知并以swift接收,ios,objective-c,swift,Ios,Objective C,Swift,试图从目标c发送通知并以swift接收。我看不到来自发送方或接收方的错误,并且该函数未在目标视图控制器中调用 视图控制器1:目标c [[NSNotificationCenter defaultCenter] postNotificationName:@"notifyme" object:self userInfo:self.profile]; 视图控制器2:swift override func viewDidLoad() { super.view

试图从目标c发送通知并以swift接收。我看不到来自发送方或接收方的错误,并且该函数未在目标视图控制器中调用

视图控制器1:目标c

    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"notifyme"
     object:self
     userInfo:self.profile];
视图控制器2:swift

override func viewDidLoad() {
    super.viewDidLoad()
    print("view did load")
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "receiveNotification:", name: "notifyme", object: nil)

}

func receiveNotification(ns: NSNotification){
    print("Received Notification")

}
按照以下步骤操作:

首先,将其添加到您的第一个视图ViewController.swift中:

override func viewDidLoad() {
    super.viewDidLoad()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "receiveNotification:", name: "notifyme", object: nil)
}

func receiveNotification(ns: NSNotification){
    print("Received Notification")
}
之后,在Obj-c中添加一个新类,不要忘记为该类创建桥接头

在桥接头文件中,通过以下方式添加并导入SecondViewController:

#import "SecondViewController.h"
在您的
SecondViewController.m
中,当您返回swift viewController时添加此代码

- (IBAction)goBack:(id)sender {

    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"notifyme"
     object:nil
     userInfo:nil];
    [self dismissViewControllerAnimated:NO completion:nil];
}
有关更多信息,请查看示例项目

更新:

如果第一个控制器为目标c,第二个/目标控制器为swift

FirstViewController.m

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(receiveTestNotification:)
                                                 name:@"TestNotification"
                                               object:nil];
}

- (void) receiveTestNotification:(NSNotification *) notification
{
    if ([[notification name] isEqualToString:@"TestNotification"])
        NSLog (@"Successfully received the test notification!");
}
@end
SecondViewController.swift

@IBAction func goBack(sender: AnyObject) {

    NSNotificationCenter.defaultCenter().postNotificationName("TestNotification", object: nil, userInfo: nil)
    self.dismissViewControllerAnimated(true, completion: nil)
}
桥接头.h

#import "FirstViewController.h"

在Swift3中,我解决了这个问题

发送OC

[[NSNotificationCenter defaultCenter] postNotificationName:"You NotificationName" object:nil];
收到Swift 3

func applicationDidBecomeActive(_ application: UIApplication) {
    NotificationCenter.default.addObserver(self, selector: #selector(self.handleNotification), name: NSNotification.Name.init(rawValue: "You NotificationName"), object: nil)
}
func handleNotification() -> Void {

}

从obj c发送并在swift上重新保存是什么意思?lolI的意思是从目标c代码发送通知并尝试从swift代码接收通知。对不起,输入错误。请检查粘贴的部分代码。如果需要进一步的信息,请告诉我。如果您有任何问题,感谢您在上面的快速响应ADD@objc注释
receiveNotification
。否则您的代码看起来很好。我相信我尝试添加了@objc,但它仍然没有被调用。何时发送通知?当ViewController 1发送通知时,两个视图控制器都初始化了吗?有点困惑。我的第一个控制器是objective c,第二个/目标控制器是swift。建议仍然适用于查看控制器1:Objcdetailed if(self.profile){[[NSNotificationCenter defaultCenter]postNotificationName:@“LoginViewdFinish”对象:self userInfo:self.profile];}UIStoryboard*MainstryBoard=[UIStoryboard故事板,名称:@“Main”bundle:nil];UIViewController*vc=[MainstryBoard实例化控件及其标识符:@“LoginViewController”];[self-presentModalViewController:vc动画:是];很抱歉让您感到困惑。第一个控制器是目标c,它是通知的发送者。接收方控制器是swift。您的示例代码非常有用,您可以共享任何示例项目吗?只是为了更新用例,我有3个视图控制器,如下所示1.视图控制器swift,2.视图控制器Objc(通知发送方),3.查看控制器Swift(通知接收方)