【发布时间】:2015-07-24 22:03:22
【问题描述】:
我一直在研究 iOS 应用程序结构,几乎完全理解了 MVC 范式,包括代理、视图和控制器。然后我在网上看到了一个例子,它只包含一个 AppDelegate 文件,没有视图、视图控制器或 nib 文件,它可以工作:
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
CGRect bounds = self.window.bounds;
view = [[UIView alloc] initWithFrame: bounds];
[view setBackgroundColor: [UIColor yellowColor]];
[self.window addSubview: view];
CGRect labelFrame = CGRectMake(30, 30, 100, 30);
label = [[UILabel alloc] initWithFrame: labelFrame];
[label setText: @"My Label"];
[label setTextAlignment: NSTextAlignmentCenter];
[label setTextColor: [UIColor orangeColor]];
[view addSubview: label];
CGRect buttonFrame = CGRectMake(vCentre - 50, hCentre - 20, 100, 30);
button = [[UIButton alloc] initWithFrame: buttonFrame];
[button setTitle: @"My Button" forState: UIControlStateNormal];
[[button titleLabel] setTextAlignment: NSTextAlignmentCenter];
[button setTitleColor: [UIColor redColor] forState: UIControlStateNormal];
[view addSubview: button];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents: UIControlEventTouchDown];
[self.window makeKeyAndVisible];
return YES;
}
在没有设置任何视图控制器或根视图控制器的情况下如何工作?这是否意味着实际上并不需要视图控制器?
【问题讨论】:
-
是的;它们并不是真正需要的。
-
能否更新您获取此代码和信息的链接?
标签: ios objective-c iphone uiview uiviewcontroller