相关疑难解决方法(0)

Swift 3 NSNotificationCenter KeyboardWill显示/隐藏

我有一段代码在Swift 2中工作,我尝试使用xCode将代码更新到最新版本,我修复了除两个问题之外的所有内容

我有这个代码

let loginvc: LoginVC = self.storyboard?.instantiateViewController(withIdentifier: "LoginVC") as! LoginVC
NotificationCenter.defaultCenter().addObserver(self, selector: #selector(LoginViewController.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil)
NotificationCenter.defaultCenter().addObserver(self, selector: #selector(LoginViewController.keyboardWillHide(_:)), name: UIKeyboardWillHideNotification, object: nil)
Run Code Online (Sandbox Code Playgroud)

与此配对

func keyboardWillShow(notification: NSNotification) {

    constraint.constant = -100
    UIView.animate(withDuration: 0.3) {
        self.view.layoutIfNeeded()
    }
}

func keyboardWillHide(notification: NSNotification) {

    constraint.constant = 25
    UIView.animate(withDuration: 0.3) {
        self.view.layoutIfNeeded()
    }
}
Run Code Online (Sandbox Code Playgroud)

在第一部分我现在得到一个错误说"类型'LoginViewController'没有成员'keyboardwillshow/hide'

我不明白为什么它没有看到下面的方法

有人知道这个问题的解决方案吗?

nsnotificationcenter ios swift swift3

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

如何在swift 2命令行工具中创建最小守护进程?

我想做什么

我想运行一个可以监听OSX系统事件的守护进程,就像NSWorkspaceWillLaunchApplicationNotificationcommand line toolxcode项目中一样?那可能吗?如果没有,为什么不,有没有任何工作或黑客?

一些代码示例

以下来自swift 2 cocoa application项目的示例代码设置了一个系统事件侦听器,WillLaunchApp每次启动OSX应用程序时都会调用该事件侦听器.(这很好用)

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
    func applicationDidFinishLaunching(aNotification: NSNotification) {
        NSWorkspace.sharedWorkspace()
            .notificationCenter.addObserver(self,
                selector: "WillLaunchApp:",
                name: NSWorkspaceWillLaunchApplicationNotification, object: nil)
    }

    func WillLaunchApp(notification: NSNotification!) {
        print(notification)
    }
}
Run Code Online (Sandbox Code Playgroud)

相比之下,这个类似的swift 2 command line tool项目不会打电话 WillLaunchApp.

import Cocoa

class MyObserver: NSObject
{
    override init() {
        super.init()
        NSWorkspace.sharedWorkspace()
            .notificationCenter.addObserver(self,
                selector: "WillLaunchApp:",
                name: NSWorkspaceWillLaunchApplicationNotification, object: nil)
    }

    func WillLaunchApp(notification: NSNotification!) { …
Run Code Online (Sandbox Code Playgroud)

macos xcode cocoa swift swift2

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

标签 统计

swift ×2

cocoa ×1

ios ×1

macos ×1

nsnotificationcenter ×1

swift2 ×1

swift3 ×1

xcode ×1