相关疑难解决方法(0)

如何使用Swift 3.0中的NotificationCenter和swift 2.0中的NSNotificationCenter传递数据?

我正在socket.io我的swift ios应用程序中实现.

目前在几个面板上我正在监听服务器并等待收到的消息.我这样做是通过调用getChatMessage每个面板中的函数来实现的:

func getChatMessage(){
    SocketIOManager.sharedInstance.getChatMessage { (messageInfo) -> Void in
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            //do sth depending on which panel user is
        })
    }
}
Run Code Online (Sandbox Code Playgroud)

但是我注意到这是一个错误的方法,我需要更改它 - 现在我想开始只收听一次传入消息,当任何消息到来时 - 将此消息传递给任何监听它的面板.

所以我想通过NSNotificationCenter传递传入的消息.到目前为止,我能够传递发生的事情的信息,但不能传递数据本身.我是这样做的:

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.showSpinningWheel(_:)), name: showSpinner, object: nil)
Run Code Online (Sandbox Code Playgroud)

然后我有一个叫做的函数:

func showSpinningWheel(notification: NSNotification) {
}
Run Code Online (Sandbox Code Playgroud)

任何时候我想打电话给我:

NSNotificationCenter.defaultCenter().postNotificationName(hideSpinner, object: self)
Run Code Online (Sandbox Code Playgroud)

那么如何传递对象messageInfo并将其包含在被调用的函数中?

nsnotifications nsnotificationcenter ios swift swift3

102
推荐指数
4
解决办法
9万
查看次数

如何在swift中通过通知传递多个值

如何通过通知发送数字和字符串...

let mynumber=1;
let mytext="mytext";
NSNotificationCenter.defaultCenter().postNotificationName("refresh", object: ?????????????);
Run Code Online (Sandbox Code Playgroud)

并在接收器中接收值?

func refreshList(notification: NSNotification){
        let receivednumber=??????????
        let receivedString=?????????
    }
Run Code Online (Sandbox Code Playgroud)

notifications ios swift

22
推荐指数
4
解决办法
3万
查看次数

使用NSNotification将NSString变量传递给其他类

我想将NSString从一个类传递到另一个类,并将NSString添加到我的第二个类中的NSMutableArray.我相信我可以使用NSNotification,但我不知道如何通过变量通知.我的代码是这样的:

//class1.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property(strong,nonatomic)NSString *variableString;

@end
Run Code Online (Sandbox Code Playgroud)

//class1.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize variableString = _variableString;

- (void)viewDidLoad
{
[super viewDidLoad];
[self setVariableString:@"test"];

[[NSNotificationCenter defaultCenter] postNotificationName: @"pasteString" object: _variableString];

// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}

@end
Run Code Online (Sandbox Code Playgroud)

//class2.h

#import <UIKit/UIKit.h>

@interface ViewController2 : UIViewController

@property(strong,nonatomic)NSMutableArray *arr;

@end
Run Code Online (Sandbox Code Playgroud)

//class2.m

#import "ViewController2.h" …
Run Code Online (Sandbox Code Playgroud)

xcode objective-c nsnotificationcenter ios

7
推荐指数
1
解决办法
2万
查看次数