swift中自定义UIApplication

原文:http://stackoverflow.com/questions/24020000/subclass-uiapplication-with-swift

1.首先定义UIApplication的子类SLApplication

import Foundation
import UIKit

class SLApplication: UIApplication {
    override func sendEvent(event: UIEvent) {
        super.sendEvent(event)
        print("send event")
    }
}

2.设置入口
新建main.swift,

import Foundation
import UIKit

UIApplicationMain(Process.argc, Process.unsafeArgv, NSStringFromClass(SLApplication), NSStringFromClass(AppDelegate))

3、去掉Appdelegate的@UIApplicationMain

@UIApplicationMain

在类的最顶部声明@UIApplicationMain,表示该类是application的delegate。如果不用该属性标识,另外一种做法是在main.swift中调用UIApplicationMain函数,设置delegate和application。以上我们就是根据这种方法来设置的。

Apple doc

Apply this attribute to a class to indicate that it is the application delegate. Using this attribute is equivalent to calling the UIApplicationMain function and passing this class’s name as the name of the delegate class.

If you do not use this attribute, supply a main.swift file with a main function that calls the UIApplicationMain(:::) function. For example, if your app uses a custom subclass of UIApplication
as its principal class, call the UIApplicationMain(
:::) function instead of using this attribute.

可以下载demo:https://github.com/silan-liu/CustomApplication

你可能感兴趣的:(swift中自定义UIApplication)