关于Appdelegate

(In swift)

The two source code files are AppDelegate.swift and ViewController.swift. In iOS, a delegate is a class that does something on behalf of another class, and the AppDelegate is a place to handle special UIApplication states. It has a bunch of functions called by iOS. For instance, if your App is in use and the phone rings, then the function applicationWillResignActive will be called. This is where you should pause tasks, disable timers, etc.

Here’s a list of functions that are stubbed in AppDelegate.swift. If your application supports background processing, then applicationDidEnterBackground is called, otherwise applicationWillTerminate. These functions are places to add code to save data, but we will only use the first one:

func applicationWillResignActive(application: UIApplication)

func applicationDidEnterBackground(application: UIApplication)

func applicationWillEnterForeground(application: UIApplication)

func applicationDidBecomeActive(application: UIApplication)

func applicationWillTerminate(application: UIApplication)

If the first function definition has NSObject: AnyObject, change it so it reads NSObject: NSDictionary. This is the function to which we need to add code; it’s where everything starts. The -> Bool means it returns a bool value, i.e. true or false. If something goes wrong in your code when the app starts, it will return “false” (but in our case, it’ll always return true).

Adding Code to AppDelegate.swift

This is what the whole AppDelegate.swift file should look like. I’ve removed comments to keep it short:

import UIKit

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {

window = UIWindow(frame: UIScreen.mainScreen().bounds)

if let window = window{

window.backgroundColor=UIColor.whiteColor()

window.makeKeyAndVisible()

window.rootViewController=ViewController()

}

return true

}

func applicationWillResignActive(application: UIApplication) {}

func applicationDidEnterBackground(application: UIApplication) {}

func applicationWillEnterForeground(application: UIApplication) {}

func applicationDidBecomeActive(application: UIApplication) {}

func applicationWillTerminate(application: UIApplication) {}

}

In Swift you create an instance of a class like this: window = UIWindow( .. ) to create an instance of UIWindow. This is just one parameter passed in, and it’s named frame with the value UIScreen.mainScreen().Bounds, which specifies the size of the whole screen. Note that window is defined in the class as UIWindow?. The question mark means it’s an optional type and that the value can be absent or present; this is similar to a nil value in other languages, but it works for all types not just classes.

The if let window = checks that the window is present and unwraps window (which is of type UIWindow?) so that the UIWindow properties and methods can be used. Try commenting out the “if let” line and the corresponding “}” (using //), and you’ll get three similar compile errors, with the first one saying that backgroundColor is not a member of UIWindow? It’s a member of UIWindow, so to get from UIWindow? To UIWindow, it needs unwrapping; add a “!” on the end of the variable name to unwrap it (window!.backgroundColor = will work). That being said, without the unwrapping provided by “if let,” each window.reference in the block would also need to be window!. “If let” does all three unwraps.

The lines within that statement set ViewController as the root view controller, which is the first one created. The view controllers you implement control the entirety of your app’s navigation. Apple’s About View Controllers is well worth reading if you want to understand the whole concept; however, in this instance, we’ll cover just one example. But what exactly does a view controller do?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值