标签: nsnotificationcenter

后台线程通知更新NSView

有一项任务需要在NSView上绘制N个图像.但是在开始时我不知道图像的数量,因此我将其设置为背景线程.

 [NSThread detachNewThreadSelector:@selector(workInBackgroundThread:) toTarget:self withObject:nil];
Run Code Online (Sandbox Code Playgroud)

当它获得图像数量时,它会发送通知

- (void)workInBackgroundThread:(id)unusedObject {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

//get image amount....

[[NSNotificationCenter defaultCenter] postNotificationName:@"gotImageAmount" object:nil];

//...
//continue to load images

}
Run Code Online (Sandbox Code Playgroud)

我尝试调整NSView的大小取决于通知功能中的图像数量

-(void)processGotImagesAmount:(NSNotification*)notification  
{

    //others codes

    [myNSView setFrame:CGRectMake(0,   0, calculateNewWidth, caculatedNewHeight)];//line A

    //others codes

}
Run Code Online (Sandbox Code Playgroud)

但当应用程序执行A行时,它会冻结,

[myNSView setFrame:CGRectMake(0,   0, calculateNewWidth, caculatedNewHeight)];
Run Code Online (Sandbox Code Playgroud)

本身没有问题,如果我点击一个bottun来调用它,它的工作原理!

但看起来它在通知功能中不起作用

欢迎任何评论

cocoa multithreading nsnotificationcenter

1
推荐指数
1
解决办法
3297
查看次数

当应用程序在 Lion 上失去焦点时,NSNotificationCenter Observer 停止接收事件

我有一个从默认 NSNotificationCenter 订阅特定类型通知的应用程序。

在 OSX Lion 上,它工作正常,只是当应用程序失去焦点(另一个应用程序变为活动状态)时,它会停止接收事件。当应用获得焦点时,它会再次开始接收事件。该应用程序在以前版本的 OSX 上没有这种行为,它总是收到通知,即使它不在焦点上。

我能做些什么来改变这种行为?

谢谢!弥敦道

cocoa nsnotification nsnotificationcenter osx-lion

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

无法识别的选择器发送到实例

我在选择通知方法时遇到问题.

在init中我已经定义了这个:

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(stopSyncIndicator:) name:IOS_STOP_SYNC_INDICATOR object:nil];
Run Code Online (Sandbox Code Playgroud)

不过,在头文件和相同的实现中都定义了该方法:

-(void)stopSyncIndicator
{
    [indicator stopAnimating];
}
Run Code Online (Sandbox Code Playgroud)

但是,当其他班级发布此通知时:

NSNotification *note = [NSNotification notificationWithName:IOS_STOP_SYNC_INDICATOR object:nil];
[[NSNotificationCenter defaultCenter] postNotification:note];
Run Code Online (Sandbox Code Playgroud)

地狱破裂了:

[FTRecordViewController stopSyncIndicator:]: unrecognized selector sent to instance 0x8d3bc00
2013-11-18 13:47:06.994 [1835:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[FTRecordViewController stopSyncIndicator:]: unrecognized selector sent to instance 0x8d3bc00'
*** First throw call stack:
(
    0   CoreFoundation                      0x01bf75e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x018f88b6 objc_exception_throw + 44
    2   CoreFoundation                      0x01c94903 -[NSObject(NSObject) doesNotRecognizeSelector:] …
Run Code Online (Sandbox Code Playgroud)

nsnotificationcenter ios ios7

1
推荐指数
1
解决办法
955
查看次数

UIView子类未收到NSNotificationCenter通知

我之前使用过NSNotificationCenter进行应用程序范围的消息传递,但今天我发现了一个奇怪的漏洞.在客户的项目上工作有一些UIView子类需要接收我已经在应用程序中的其他地方发布/接收的NSNotifications.

我从init方法调用addObserver:self并使用#defined常量作为通知名称.当我注册接收的通知被发布时,正在侦听的所有其他对象(所有UIViewController子类)都会获得除我的UIView子类之外的通知.

有没有人成功使用过UIView子类中的NSNotificationCenter?我已经尝试过断点和NSLog,但我的@selector永远不会被调用.

编辑 - 视图是使用loadNibNamed创建的:并且initWithCoder:未在类中实现.在initWithCoder中注册通知:效果很好.

objective-c uiview nsnotification nsnotificationcenter ios

1
推荐指数
1
解决办法
1225
查看次数

removeObserver不适用于UIApplicationWillEnterForegroundNotification IOS

嗨,我正在开发iPhone应用程序,我正在注册UIApplicationWillEnterForegroundNotification以进行通知.但是当我试图取消注册那个观察者时,它就无法工作了.它仍然被调用.我的代码看起来像

在viewDidLoad方法我正在这样注册

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

在我的注销方法中,我试图像这样删除这个观察者

 [[NSNotificationCenter defaultCenter] removeObserver:UIApplicationWillEnterForegroundNotification];
Run Code Online (Sandbox Code Playgroud)

但这不起作用.难道我做错了什么.需要一些帮助.谢谢.

iphone nsnotificationcenter

1
推荐指数
1
解决办法
1177
查看次数

UIMoviePlayerControllerDidExitFullscreenNotification NSNotification在iOs 8中不起作用

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

UIMoviePlayerControllerDidExitFullscreenNotification NSNotification在iOs 8中不起作用

iphone nsnotificationcenter ipad ios ios8

1
推荐指数
1
解决办法
999
查看次数

如何使NSNotificationCenter仅在选择某个UITextField时运行

NSNotificationCenter当键盘显示时,我用来移动主视图框.

但是,我只想让它在UITextField选择一个时工作.

这是我到目前为止:

func getKeyboardHeight(notification: NSNotification) -> CGFloat {
    let userInfo = notification.userInfo

    let keyboardSize = userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue

    return keyboardSize.CGRectValue().height

}

func keyboardWillShow(notification: NSNotification) {
    self.view.frame.origin.y -= getKeyboardHeight(notification)
}

func keyboardWillHide(notification: NSNotification) {
    self.view.frame.origin.y += getKeyboardHeight(notification)
}

func subscribeToKeyboardNotifications() {
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
}

func unsubscribeToKeyboardNotifications() {
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
}

func subscribeToKeyboardHide() {
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
}

func unsubscribeToKeyboardHide() {
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil) …
Run Code Online (Sandbox Code Playgroud)

iphone cocoa nsnotificationcenter ios swift

1
推荐指数
1
解决办法
420
查看次数

使用NSNotificationCenter时,ARC无法正常工作

为什么不在使用NSNotificationCenter的对象上调用deist,我在下面包含了我的代码的简单版本.在我创建一个观察通知的对象以及触发通知时,它会删除观察者的订阅.如果释放了对象,我也会删除订阅.但是,在为应用程序运行性能分析时,您可以看到在viewDidAppear完成后,测试对象的持久分配现在为零,应该已经释放.为什么会这样?

import UIKit

class ViewController: UIViewController {

    var t: test?

    override func viewWillAppear(animated: Bool) {
        t = test()
        fire()
        t = nil
    }

    func fire() {
        NSNotificationCenter.defaultCenter().postNotificationName("Hello",
            object: nil)
    }

}

class test {

    var e: NSObjectProtocol?

    init() {
        e = NSNotificationCenter.defaultCenter().addObserverForName(
            "Hello", object: nil, queue: NSOperationQueue.mainQueue(),
            usingBlock: sayHello)
    }
    deinit {
        if let e = e { NSNotificationCenter.defaultCenter().removeObserver(e) }
    }

    func sayHello(notification: NSNotification) {
        if let e = e { NSNotificationCenter.defaultCenter().removeObserver(e) }
    }

}
Run Code Online (Sandbox Code Playgroud)

即使在Objective-C中,我也会很感激,因为它也可能会回答这个问题.

非常感谢你

nsnotificationcenter ios swift

1
推荐指数
1
解决办法
117
查看次数

NotificationCenter还是关闭?

对于主管:在决定问这个问题之前,我进行了很多搜索,但没有找到我想要的。

我的问题是:使用NotificationCenter和使用Closures两个实例之间进行通信之间哪个更好?如果它们相似,我应该怎么用?

希望这个例子可以帮助您更好地理解:

如果我们以URLSession课堂为例。为什么大多数方法都有闭包?他们为什么不在其中发送通知Data, Response, and Error

我知道它们之间的区别,我只是不知道在什么情况下应该使用每个。

closures nsnotificationcenter swift

1
推荐指数
1
解决办法
745
查看次数

Xcode调试器(lldb)从内存地址获取对象描述

鲜为人知的事实:现在,可以在Xcode中将其暂停在调试器中时,对通知中心进行自省,以了解注册了哪些对象以接收哪些通知:

(lldb) po NotificationCenter.default

<NSNotificationCenter:0x6040000d40b0>
Name, Object, Observer, Options
com.apple.accessibility.reduce.motion.status, 0x10b126190, 0x7fc795700140, 1001
com.apple.accessibility.asst.scanner.status, 0x10b126190, 0x7fc795700140, 1001
// ... etc. ...
Run Code Online (Sandbox Code Playgroud)

很不错,但我如何从存储器地址获得0x7fc795700140学习什么对象

编辑我在这里留下这个问题,但是我认为正确的答案是在LLDB(Swift)上显示的那种:将原始地址转换为可用类型

xcode nsnotificationcenter lldb

1
推荐指数
3
解决办法
2337
查看次数