标签: nsnotificationcenter

检测iOS中的旋转更改

我正在制作一个iOS应用程序,需要在旋转时进行一些界面重新排列.我试图通过实现检测到这一点- (void)orientationChanged:(NSNotification *)note,但这会给我通知设备面朝上或面朝下的时间.

我想要一种方法来在界面改变方向时得到通知.

interface nsnotificationcenter screen-rotation ios auto-rotation

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

实例已被解除分配,而关键价值观察员仍在其中注册

我有一个UITableView.

在这里,我得到了不同的细胞.每个细胞都有一个模型.使用KVO和NotificationCenter,单元格会监听模型的更改.当我离开ViewController时,我收到此错误:

An instance 0x109564200 of class Model was deallocated while key value observers were still registered with it. 
Observation info was leaked, and may even become mistakenly attached to some other object. 
Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info:
<NSKeyValueObservationInfo 0x109429cc0> (
<NSKeyValueObservance 0x109429c50: Observer: 0x10942d1c0, Key path: name, Options: <New: NO, Old: NO, Prior: NO> Context: 0x0, Property: 0x10968fa00>
)
Run Code Online (Sandbox Code Playgroud)

在单元格中,我在设置/更改模型属性时执行此操作:

[_model addObserver:self
         forKeyPath:@"name"
            options:0
            context:nil];

[[NSNotificationCenter defaultCenter] addObserver:self …
Run Code Online (Sandbox Code Playgroud)

objective-c nsnotificationcenter ios

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

键盘上的NSNotificationCenter Swift 3.0显示和隐藏

我试图在键盘显示和消失时运行一个函数,并具有以下代码:

let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: #selector(ViewController.keyBoardUp(Notification :)), name:  NSNotification.Name.UIKeyboardWillShow, object: nil)
Run Code Online (Sandbox Code Playgroud)

以下功能keyBoardUp:

func keyBoardUp( Notification: NSNotification){
    print("HELLO")
}
Run Code Online (Sandbox Code Playgroud)

但是,当键盘显示时,该功能不会打印到控制台.非常感谢帮助

keyboard-events nsnotificationcenter ios swift

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

iOS iMessage扩展截图检测

请使用Swift 4+

注意:我在iMessage扩展程序而不是标准iMessage视图中时正在检测屏幕截图。

更新-我想出了一个可行的解决方案,每隔0.3秒左右在敏感信息周期内检查一次照片库,以检查是否添加了新的屏幕截图。如果用户未授予照片库许可,则在他们启用照片库之前,不会向他们显示内容。但是,我仍在寻找其他创新解决方案,这些解决方案不一定涉及如此繁琐的过程。

我有一个iMessage扩展名,我正在尝试检测屏幕截图。我已经尝试过在网上找到的每个观察者,由于某种原因,它没有注册屏幕截图。

ViewWillAppear()

UIScreen.main.addObserver(self, forKeyPath: "captured", options: .new, context: nil)
Run Code Online (Sandbox Code Playgroud)

观察者

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
    if (keyPath == "captured") {
        let isCaptured = UIScreen.main.isCaptured
        print(isCaptured)
        screenshot()
        //screenshot() sends a message alerting the message was screens hotted. However, the print statement didn't even run.
    }
}
Run Code Online (Sandbox Code Playgroud)

ViewWillDisappear()

UIScreen.main.removeObserver(self, forKeyPath: "captured", context: nil)
Run Code Online (Sandbox Code Playgroud)

我还尝试了标准的默认通知中心

let mainQueue = OperationQueue.main
    NotificationCenter.default.addObserver(forName: UIApplication.userDidTakeScreenshotNotification, object: nil, queue: mainQueue) { notification in
        // executes …
Run Code Online (Sandbox Code Playgroud)

nsnotificationcenter ios imessage swift imessage-extension

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

当postNotificationName:调用时,不发送NSNotification

我试图让使用的一个实例NSNotificationCenteraddObserverpostNotificationName,但我不能工作了,为什么它不会工作.

我有2行代码来添加观察者并在2个不同的类中发送消息

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

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

如果我将名称设置为nil它工作正常,因为它只是一个广播,当我尝试定义通知名称时,消息永远不会通过.

iphone objective-c nsnotificationcenter

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

如何检查当前是否正在显示UIViewController?

如何检查UIViewController当前是否正在显示?

UIViewControllers正在倾听NSNotifications- 即使它们没有显示(即未显示).所以,我可以有10个UIViewController在背景中观察NSNotificationsNSNotificationCenter.当a NSNotification发布和接收时UIViewController,我想知道它是否正在显示.如果不是,我将设置一个布尔值,以便在显示视图时处理它.如果它当前正在显示,我会做更多的事情,比如立即更新表,等等......

objective-c uiviewcontroller nsnotification nsnotificationcenter ios

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

如何编写NSNotification的单元测试

我在swift工作,我想刷新一个页面,所以我使用通知发送它,我在一个ViewController中发布通知并在另一个中添加观察者,它工作正常.我想要做的是在swift中添加单元测试.我查了很多网站但是没能做到.我是新手,不知道从哪里开始.

基本上工作是,当我点击按钮通知发布时,当加载下一个视图控制器时,添加通知观察器.

我该怎么做单元测试

提前致谢

编辑:代码

NSNotificationCenter.defaultCenter().postNotificationName("notificationName", object: nil)
Run Code Online (Sandbox Code Playgroud)

并添加观察者

NSNotificationCenter.defaultCenter().addObserver(self, selector: "vvv:",name:"notificationName", object: nil)
Run Code Online (Sandbox Code Playgroud)

unit-testing nsnotificationcenter ios swift ios8

14
推荐指数
2
解决办法
7458
查看次数

连接Lightning kBD或Smart KBD后没有EAAccessoryDidConnectNotification和EAAccessoryDidDisconnectNotification

我不能让这两个通知EAAccessoryDidConnectNotificationEAAccessoryDidDisconnectNotification连接闪电大骨节病和大骨节病灵动与我的应用程序后.但它适用于iOS9.3,我的代码如下,

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_accessoryDidConnect:) name:EAAccessoryDidConnectNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_accessoryDidDisconnect:) name:EAAccessoryDidDisconnectNotification object:nil];
[[EAAccessoryManager sharedAccessoryManager] registerForLocalNotifications];
Run Code Online (Sandbox Code Playgroud)

原因是什么?

PS:我在日志中收到以下消息.找不到" com.apple.private.externalaccessory.showallaccessories "权利

objective-c nsnotificationcenter external-accessory ios

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

iphone通知导致"无法识别的选择器发送到实例..."

为了简短起见,我NSNotificationClassA(in viewDidLoad)中注册了以下监听器:

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

我有选择器声明ClassA.h:

- (void)playSong:(NSNotification *) notification;
Run Code Online (Sandbox Code Playgroud)

实施如下:

- (void)playSong:(NSNotification *) notification {
    NSString *theTitle = [notification object]; 
    NSLog(@"Play stuff", theTitle);
}
Run Code Online (Sandbox Code Playgroud)

ClassB(在tableView:didSelectRowAtIndexPath:方法中)我有:

NSInteger row = [indexPath row];
NSString *stuff = [playlistArray objectAtIndex:row];
[[NSNotificationCenter defaultCenter] postNotificationName:@"playNotification" object:stuff];
Run Code Online (Sandbox Code Playgroud)

最后都会收到一条错误消息:

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

playSong调用方法之前.

有人可以帮帮我吗?从一个控制器向另一个控制器发布通知时我忘记了什么?

iphone objective-c nsnotificationcenter ios

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

何时使用NSNotificationCenter

我希望在一个对象的多个事件上有多个观察者(1对N关系).

实现这一任务的机制由提供NSNotificationCenter.当用于我的问题时,该机制看起来相当矫枉过正.

如何在不使用以下情况下手动完成NSNotificationCenter:

- (void)addDelegate:(id<DelegateProtocol>)delegate;
- (void)removeDelegate:(id<DelegateProtocol>)delegate;
Run Code Online (Sandbox Code Playgroud)

从我的对象添加和删除观察者.

- (void)someEventFired:(NSObject<NSCopying> *)eventData
{
    for (id delegate in delegates) {
        NSObject *data = [eventData copy];
        [delegate someEventFired:data];
    }
}
Run Code Online (Sandbox Code Playgroud)

这种机制是直接的,易于实现,而对象不必共享其他字符串.

  • 除了NSNotificationCenter?之外,iOS框架中是否存在1对N代表(如C#事件)的官方模式?
  • 什么时候应该NSNotificationCenter使用,何时不使用?
  • 什么时候应该使用像我在这里建议的实现,什么时候不使用?

architecture objective-c nsnotificationcenter ios observer-pattern

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