我如何找出NSTabViewItem何时更改,即用户已更改NSTabView的视图。
理想情况下,我想生成一个通知,但是任何解决方案都将受到欢迎。
谢谢!
我通过默认中心发布通知,如下所示:
NSNotificationCenter.defaultCenter().postNotificationName(ColorDidGetTappedNotification, object: self)
Run Code Online (Sandbox Code Playgroud)
我在另一个实例中观察通知,如下所示:
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("colorDidGetTapped:"), name: ColorDidGetTappedNotification, object: nil)
....
func colorDidGetTapped(notification: NSNotification) {
println("Notification recieved")
}
Run Code Online (Sandbox Code Playgroud)
但我得到一个无法识别的选择器异常:
更新1
*** NSForwarding: warning: object 0x7d564490 of class '_D.GameModel' does not implement methodSignatureForSelector: -- trouble ahead
Unrecognized selector -[_D.GameModel colorDidGetTapped:]
Run Code Online (Sandbox Code Playgroud) 我有一个视图控制器,可以在 ViewWillAppear 上创建 UITextField firstResponder。通常我可以依靠 UIKeyboardWillShow 通知来检测键盘是否已显示,但如果我在键盘已经显示时进入当前视图控制器,则不会触发。
谁有想法?
您能解释一下NSNotification的目的是什么,我可以使用哪些情况?
通知是否通过传递委托调用应用程序中的所有类,还是调用特定类?
是否可以创建1个通知,并在多个类中接收它?
我可以使用一些建筑建议.我现在遇到了几次以下问题,我从来没有找到一种真正优雅的方法来解决它.
问题,尽可能在最高级别描述:
我有一个父类,希望充当多个孩子的代理(都使用相同的协议),但是当孩子们在父母上调用方法时,父母不再知道哪个孩子正在打电话.
我想使用松散耦合(委托/协议或通知)而不是直接调用.我不需要多个处理程序,因此通知看起来可能有点矫枉过正.
为了说明这个问题,让我尝试一个超简化的例子:
我从父视图控制器(和相应的视图)开始.我创建了三个子视图,并将它们中的每一个插入到父视图中.我想在用户触摸其中一个孩子时通知父视图控制器.有几个选项可以通知父母:
定义协议.父实现协议并将其自身设置为每个子代的委托.当用户触摸子视图时,其视图控制器调用其委托(父代).在这种情况下,通知父母一个视图被触摸,但它不知道哪一个.还不够好.
与#1相同,但定义协议中的方法也传递某种标识符.当孩子告诉其代表它被触摸时,它也会传递指向自身的指针.这样,父母就知道确切地触摸了哪个视图.对象传递自身的引用似乎很奇怪.
使用NSNotifications.父级为三个子节点中的每一个定义一个单独的方法,然后为这三个子节点中的每一个订阅"viewWasTouched"通知作为通知发送方.孩子们不需要将自己附加到用户词典,但他们确实需要发送带有指针的通知作为范围.
与#4相同,但不是使用单独的方法,父级可以使用带有switch case或其他分支的方法以及通知的发送方来确定要采用的路径.
创建多个中间人类,充当子视图的委托,然后使用指向子节点的指针或其他一些区分因子调用父节点上的方法.这种方法似乎不具备可扩展性.
这些方法中的任何一种都是最佳实践吗?我不能肯定地说,但感觉我错过了一些更明显/优雅的东西.
delegates protocols objective-c nsnotifications nsnotification
我正在寻找像mac com.apple.iTunes.playerInfo这样的mac上存在的分布式通知列表,但我找不到任何东西.我想知道Safari或QuickTime等其他应用程序是否有这样的通知?如果他们存在什么是他们的通知名称?
问候
我的帖子通知功能有问题.
在FirstViewController中viewDidLoad我有这样的句子:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "ponresultado", name: "resultadobusqueda", object: nil)
Run Code Online (Sandbox Code Playgroud)
之后我有了这个功能:
func ponresultado(notification:NSNotification)
{
var oDato : oDatoSel = notification.object as oDatoSel
}
Run Code Online (Sandbox Code Playgroud)
在类型的第二视图控制器TableViewController在didDeselectRowAtIndexPath方法我有这样的代码:
var oDato : oDatoSel = oDatoSel()
oDato.id = "1"
oDato.nombre = "test"
NSNotificationCenter.defaultCenter().postNotificationName("resultadobusqueda", object: oDato)
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
[App.FirstViewController ponresultado]: unrecognized selector sent to instance 0x797d2310
Run Code Online (Sandbox Code Playgroud)
如果在我的ponresultado函数中FirstViewController,我退出这样的notification:NSNotification参数:
func ponresultado()
{
var oDato : oDatoSel = notification.object as oDatoSel
}
Run Code Online (Sandbox Code Playgroud)
我没有错误.为什么?
nsnotification nsnotificationcenter ios unrecognized-selector swift
在AlamoFire获取请求中,我正在尝试更新我的进度条.像这样:
alamofireManager.request(.GET, urlPath, parameters: params).responseJSON{(request,response,JSON,error) in
...<code here>...
dispatch_async(dispatch_get_main_queue(), {
NSNotificationCenter.defaultCenter().postNotificationName(LoginVCHideSpinnerAndShowProgressBarName as String, object: self)
})
...<more code here>...
}
Run Code Online (Sandbox Code Playgroud)
由于某些原因,这需要几秒钟的时间来执行,如果我使用dispatch_sync该应用程序似乎只是卡在那一点,但UI不会冻结(活动指示器微调器继续).我还想指出,一旦应用程序点击此代码,它就会继续执行代码,就像它已被执行一样.然后在大约6秒后执行它,好像它没有在主线程上被调用一样.
我也只是尝试这样做而不是使用通知.
dispatch_async(dispatch_get_main_queue(), {
loginVC.progressBar.hidden = false
loginVC.indicator.hidden = true
loginVC.progressBar.setProgress(0.1, animated: true)
})
Run Code Online (Sandbox Code Playgroud)
这似乎比通知慢.
我很困惑为什么会发生这种情况,因为我告诉它在主线程中进行更新.我也很困惑为什么通知实际上要快一点.
uiprogressview nsnotification dispatch-async swift alamofire
在Swift 3中,我使用以下代码注册了睡眠和唤醒通知:
let notificationCenter = NSWorkspace.shared.notificationCenter
notificationCenter.addObserver(self, selector: #selector(AppDelegate.sleepListener), name: NSNotification.Name.NSWorkspaceWillSleep, object: nil)
notificationCenter.addObserver(self, selector: #selector(AppDelegate.wakeUpListener), name: NSNotification.Name.NSWorkspaceDidWake, object: nil)
Run Code Online (Sandbox Code Playgroud)
但在迁移到Swift 4后,我在应用建议的修复程序后出现此错误:
Type 'NSNotification.Name' has no member 'NSWorkspace'
Run Code Online (Sandbox Code Playgroud)
我如何在Swift 4中执行此操作?
我正在使用AVQueuePlayer播放远程音频文件列表。我想默认实现重复所有。
我的方法是,我正在观察AVPlayerItemDidPlayToEndTime通知,并在播放完毕后将 playerItem 添加到队列的后面。
在nextAudio(notification: Notification)不运行在所有。需要这方面的帮助,或者更好的方式来实现无限游戏。
func playAudio(_ items: [AVPlayerItem]) {
let avPlayerVC = AVPlayerViewController()
let player = AVQueuePlayer(items: items)
player.actionAtItemEnd = .pause
avPlayerVC.player = player
for item in items {
NotificationCenter.default.addObserver(self,
selector: #selector(FilesViewController.nextAudio(notification:)),
name: .AVPlayerItemDidPlayToEndTime, object: item)
}
present(avPlayerVC, animated: true) {
self.player.play()
}
}
@objc func nextAudio(notification: Notification) {
debugPrint("nextAudio was called")
guard player != nil else { return }
debugPrint("AVPlayerItemDidPlayToEndTime notif info \(notification.userInfo)")
if let currentItem = notification.userInfo!["object"] as? AVPlayerItem { …Run Code Online (Sandbox Code Playgroud) nsnotification ×10
swift ×5
ios ×4
cocoa ×2
objective-c ×2
alamofire ×1
avplayer ×1
cocoa-touch ×1
delegates ×1
iphone ×1
keyboard ×1
macos ×1
nstabview ×1
nsworkspace ×1
protocols ×1
safari ×1
swift4 ×1
uitextfield ×1
xcode ×1