iOS UIKit :UIWindow
uiqwindow定义了一个window对象来管理views。一个软件只能有一个window。window的主要职能使为view提供显示取和向view传递事件。想要改变软件显示的内容,你可以改变window的root view。
uiwindow的screen属性指定了window的显示属性包括:bounds, mode, and brightness.
window notifications用来监听window 和 screen的改变,包括:
uiwindowdidbecomevisiblenotification
uiwindowdidbecomehiddennotification
uiwindowdidbecomekeynotification
uiwindowdidresignkeynotification uiwindow继承自uiview,关于这一点可能有点逻辑障碍,画框怎么继承自画布呢?不要过于去专牛角尖,画框的形状不就是跟画布一样吗?拿一块画布然后用一些方法把它加强,是不是可以当一个画框用呢?这也是为什么 一个view可以直接加到另一个view上去的原因了。一个应用程序只能有一个画框。
看一下的初始化过程(在application didfinishlauchingwithoptions里面):
- (bool)application:(uiapplication *)application willfinishlaunchingwithoptions:(nsdictionary *)launchoptions {
uiwindow *window = [[uiwindow alloc] initwithframe:[[uiscreen mainscreen] bounds]];
myviewcontroller = [[myviewcontroller alloc] init];
window.rootviewcontroller = myviewcontroller;
[window makekeyandvisible];
return yes;
}
创建一个an external display:
- (void)checkforexistingscreenandinitializeifpresent
{
if ([[uiscreen screens] count] > 1)
{
// get the screen object that represents the external display.
uiscreen *secondscreen = [[uiscreen screens] objectatindex:1];
// get the screen's bounds so that you can create a window of the correct size.
cgrect screenbounds = secondscreen.bounds;
self.secondwindow = [[uiwindow alloc] initwithframe:screenbounds];
self.secondwindow.screen = secondscreen;
// set up initial content to display...
// show the window.
self.secondwindow.hidden = no;
}
}
- (void)setupscreenconnectionnotificationhandlers
{
nsnotificationcenter *center = [nsnotificationcenter defaultcenter];
[center addobserver:self selector:@selector(handlescreendidconnectnotification:)
name:uiscreendidconnectnotification object:nil];
[center addobserver:self selector:@selector(handlescreendiddisconnectnotification:)
name:uiscreendiddisconnectnotification object:nil];
}
- (void)handlescreendidconnectnotification:(nsnotification*)anotification
{
uiscreen *newscreen = [anotification object];
cgrect screenbounds = newscreen.bounds;
if (!self.secondwindow)
{
self.secondwindow = [[uiwindow alloc] initwithframe:screenbounds];
self.secondwindow.screen = newscreen;
// set the initial ui for the window.
}
}
- (void)handlescreendiddisconnectnotification:(nsnotification*)anotification
{
if (self.secondwindow)
{
// hide and then delete the window.
self.secondwindow.hidden = yes;
self.secondwindow = nil;
}
}