我正在制作一个iOS应用程序,需要在旋转时进行一些界面重新排列.我试图通过实现检测到这一点- (void)orientationChanged:(NSNotification *)note,但这会给我通知设备面朝上或面朝下的时间.
我想要一种方法来在界面改变方向时得到通知.
interface nsnotificationcenter screen-rotation ios auto-rotation
我有一个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) 我试图在键盘显示和消失时运行一个函数,并具有以下代码:
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)
但是,当键盘显示时,该功能不会打印到控制台.非常感谢帮助
请使用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与addObserver和postNotificationName,但我不能工作了,为什么它不会工作.
我有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它工作正常,因为它只是一个广播,当我尝试定义通知名称时,消息永远不会通过.
如何检查UIViewController当前是否正在显示?
我UIViewControllers正在倾听NSNotifications- 即使它们没有显示(即未显示).所以,我可以有10个UIViewController在背景中观察NSNotifications的NSNotificationCenter.当a NSNotification发布和接收时UIViewController,我想知道它是否正在显示.如果不是,我将设置一个布尔值,以便在显示视图时处理它.如果它当前正在显示,我会做更多的事情,比如立即更新表,等等......
objective-c uiviewcontroller nsnotification nsnotificationcenter ios
我在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) 我不能让这两个通知EAAccessoryDidConnectNotification和EAAccessoryDidDisconnectNotification连接闪电大骨节病和大骨节病灵动与我的应用程序后.但它适用于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 "权利
为了简短起见,我NSNotification在ClassA(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调用方法之前.
有人可以帮帮我吗?从一个控制器向另一个控制器发布通知时我忘记了什么?
我希望在一个对象的多个事件上有多个观察者(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
ios ×9
objective-c ×6
swift ×3
iphone ×2
architecture ×1
imessage ×1
interface ×1
ios8 ×1
unit-testing ×1