以编程方式设置 root VC
Setting root VC programmatically
我是一个 Swift 初学者,请放轻松。我会在我的问题中具体说明。
我删除了故事板(我想学习如何使用 Swift 以编程方式构建 UI)。
下面的代码放在AppDelegate.swift
我在项目浏览器中也有ViewController.swift。
首先,有没有办法使用 UINavigationController.. 以外的任何东西?或者必须要有 UINavigationController..?
如果不是必须的,我怎样才能将它引用到 ScrollView 之类的例子..?
2,随着进一步研究Apple自己的指南,他们说我也可以使用window?.isHidden = false
...使用前一行和window?.makeKeyAndVisible()
..?
抱歉,如果我的问题在编程上没有意义,就像我说的,我是初学者,但我决心理解为什么我编写或复制的代码。
谢谢。
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = UINavigationController (rootViewController: ViewController())
return true
}
...
}
A window 需要一个视图控制器。它不一定是 UINavigation 控制器,但它确实需要是视图控制器。并非所有应用程序都以导航控制器开头。事实上,您可以通过查看 Xcode 在新项目中为您提供的情节提要自己看到这一点。这只是一个ViewController。您可以改用 ScrollView 吗?如果你把 scrollview(它是一个 UIView)放到一个 UIViewController,当然可以。
隐藏 window 只会让它出现和消失。使它成为键 window 意味着它将是 window 接收事件。您可能认为它在 iOS 个应用中并不重要,但 iOS 个应用可以有多个 window。在启动应用程序的情况下,最好定义 window 可见的键,而不仅仅是可见的
Firstly, is there a way to use anything other then UINavigationController..? Or having a UINavigationController is a must requirement..?
您可以使用任何 UIViewController
子类作为根视图控制器。例如UITabBarController
、UIPageViewController
、UIViewController
、UITableViewController
...
how can I just refer it to a ScrollView for example..?
您不能将 UIScrollView
设置为根视图控制器。但是,您可以将 UIScrollView
添加为 UIWindow
的子视图。不过我不建议你这样做,因为使用 VC 会使你的代码更易于管理,不同的 类 管理不同的视图。
Is there a difference between using the former line, and window?.makeKeyAndVisible()..?
是的,如果您查看 makeKeyAndVisible
的 docs:
This is a convenience method to show the current window and position it in front of all other windows at the same level or lower. If you only want to show the window, change its
isHidden
property to false
.
是的,调用 makeKeyAndVisible
将使 window 变为 "key window"。
根据 here,键 window 的行为如下:
The key window receives keyboard and other non-touch related events. Only one window at a time may be the key window.
我是一个 Swift 初学者,请放轻松。我会在我的问题中具体说明。
我删除了故事板(我想学习如何使用 Swift 以编程方式构建 UI)。
下面的代码放在AppDelegate.swift
我在项目浏览器中也有ViewController.swift。
首先,有没有办法使用 UINavigationController.. 以外的任何东西?或者必须要有 UINavigationController..?
如果不是必须的,我怎样才能将它引用到 ScrollView 之类的例子..?
2,随着进一步研究Apple自己的指南,他们说我也可以使用window?.isHidden = false
...使用前一行和window?.makeKeyAndVisible()
..?
抱歉,如果我的问题在编程上没有意义,就像我说的,我是初学者,但我决心理解为什么我编写或复制的代码。 谢谢。
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = UINavigationController (rootViewController: ViewController())
return true
}
...
}
A window 需要一个视图控制器。它不一定是 UINavigation 控制器,但它确实需要是视图控制器。并非所有应用程序都以导航控制器开头。事实上,您可以通过查看 Xcode 在新项目中为您提供的情节提要自己看到这一点。这只是一个ViewController。您可以改用 ScrollView 吗?如果你把 scrollview(它是一个 UIView)放到一个 UIViewController,当然可以。
隐藏 window 只会让它出现和消失。使它成为键 window 意味着它将是 window 接收事件。您可能认为它在 iOS 个应用中并不重要,但 iOS 个应用可以有多个 window。在启动应用程序的情况下,最好定义 window 可见的键,而不仅仅是可见的
Firstly, is there a way to use anything other then UINavigationController..? Or having a UINavigationController is a must requirement..?
您可以使用任何 UIViewController
子类作为根视图控制器。例如UITabBarController
、UIPageViewController
、UIViewController
、UITableViewController
...
how can I just refer it to a ScrollView for example..?
您不能将 UIScrollView
设置为根视图控制器。但是,您可以将 UIScrollView
添加为 UIWindow
的子视图。不过我不建议你这样做,因为使用 VC 会使你的代码更易于管理,不同的 类 管理不同的视图。
Is there a difference between using the former line, and window?.makeKeyAndVisible()..?
是的,如果您查看 makeKeyAndVisible
的 docs:
This is a convenience method to show the current window and position it in front of all other windows at the same level or lower. If you only want to show the window, change its
isHidden
property tofalse
.
是的,调用 makeKeyAndVisible
将使 window 变为 "key window"。
根据 here,键 window 的行为如下:
The key window receives keyboard and other non-touch related events. Only one window at a time may be the key window.