标签: nsnotificationcenter

通过Objective-C中的NSNotificationCenter发送和接收消息?

我试图通过NSNotificationCenterObjective-C 发送和接收消息.但是,我还没有找到任何关于如何做到这一点的例子.你如何通过发送和接收消息NSNotificationCenter

objective-c nsnotificationcenter ios

602
推荐指数
6
解决办法
38万
查看次数

NSVotificationCenter在Swift中添加了addObserver

如何在Swift中将观察者添加到默认通知中心?我正在尝试移植这行代码,以便在电池电量发生变化时发送通知.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryLevelChanged:) name:UIDeviceBatteryLevelDidChangeNotification object:nil];
Run Code Online (Sandbox Code Playgroud)

nsnotificationcenter ios swift

382
推荐指数
9
解决办法
27万
查看次数

使用Swift使用键盘移动视图

我有一个应用程序,在视图的下半部分有一个文本字段.这意味着当我输入文本字段时键盘覆盖文本字段.

如何在打字时向上移动视图以便我可以看到我正在键入的内容,然后在键盘消失时将其移回原来的位置?

我到处都看,但所有解决方案似乎都在Obj-C中,我还没有完全转换.

任何帮助将不胜感激.

uikit uikeyboard nsnotificationcenter ios swift

257
推荐指数
15
解决办法
17万
查看次数

如何使用NSNotificationCenter传递对象

我试图将我的app委托中的对象传递给另一个类中的通知接收器.

我想传递整数messageTotal.现在我有:

在接收器中:

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

- (void)viewDidLoad {
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dismissSheet) name:UIApplicationWillResignActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveTestNotification:) name:@"eRXReceived" object:nil];
Run Code Online (Sandbox Code Playgroud)

在正在进行通知的类中:

[UIApplication sharedApplication].applicationIconBadgeNumber = messageTotal;
[[NSNotificationCenter defaultCenter] postNotificationName:@"eRXReceived" object:self];
Run Code Online (Sandbox Code Playgroud)

但我想将对象传递messageTotal给另一个类.

cocoa-touch objective-c nsnotifications nsnotificationcenter swift

123
推荐指数
5
解决办法
10万
查看次数

如何在Swift 3中创建自定义通知?

在Objective-C中,自定义通知只是一个简单的NSString,但在WWDC版本的Swift 3中它并不明显.

nsnotifications nsnotification nsnotificationcenter swift swift3

110
推荐指数
6
解决办法
4万
查看次数

如何使用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万
查看次数

Android相当于NSNotificationCenter

在将iPhone应用程序移植到android的过程中,我正在寻找在应用程序内进行通信的最佳方式.意图似乎是要走的路,这是最好的(唯一的)选择吗?NSUserDefaults在性能和编码方面的重量似乎比Intents轻得多.

我还应该添加我有一个状态的应用程序子类,但我需要让另一个活动知道一个事件.

iphone events android nsnotificationcenter ios

92
推荐指数
4
解决办法
2万
查看次数

iOS 11 - 键盘高度在键盘通知中返回0

我一直在使用键盘通知没有任何问题,并获得键盘的准确高度.

- (void)keyboardDidShow:(NSNotification *) notification{
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    NSLog(@"%f",keyboardSize.height);}
Run Code Online (Sandbox Code Playgroud)

但是在iOS 11中,调用通知时键盘的大小为0.

这种情况下出现的问题是什么?我正在使用xcode 9 Beta 5

objective-c nsnotificationcenter ios

81
推荐指数
5
解决办法
2万
查看次数

如何使用NSNotificationcenter的object属性

有人可以告诉我如何在NSNotifcationCenter上使用object属性.我希望能够使用它将整数值传递给我的selector方法.

这就是我在UI视图中设置通知监听器的方法.看到我希望传递一个整数值,我不知道用什么代替nil.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveEvent:) name:@"myevent" object:nil];


- (void)receiveEvent:(NSNotification *)notification {
    // handle event
    NSLog(@"got event %@", notification);
}
Run Code Online (Sandbox Code Playgroud)

我从这样的另一个班级发出通知.该函数传递一个名为index的变量.这是我希望通过通知以某种方式启动的值.

-(void) disptachFunction:(int) index
{
    int pass= (int)index;

    [[NSNotificationCenter defaultCenter] postNotificationName:@"myevent" object:pass];
    //[[NSNotificationCenter defaultCenter] postNotificationName:<#(NSString *)aName#>   object:<#(id)anObject#>
}
Run Code Online (Sandbox Code Playgroud)

iphone cocoa objective-c nsnotificationcenter

72
推荐指数
2
解决办法
6万
查看次数

NSNotificationCenter的帖子导致"EXC_BAD_ACCESS"异常

A UIViewController将自己添加到默认中心:

[[NSNotificationCenter defaultCenter]
 addObserver:self
 selector:@selector(editFood)
 name:@"editFood"
 object:nil];
Run Code Online (Sandbox Code Playgroud)

然后UITableView代表NSObject发布一个NSNotification:

[[NSNotificationCenter defaultCenter]
 postNotificationName:@"editFood"
 object:self];
Run Code Online (Sandbox Code Playgroud)

在运行时,它会获得EXC_BAD_ACCESS异常.

是在defaultCenter哪里获得释放?当我从UIViewController向UIViewController发布通知时,同样的概念也有效,但这不重要,对吧?

iphone exc-bad-access objective-c nsnotificationcenter ios

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