以编程方式设置视图
Setting the view programmatically
我试图在 xcode 和 objective c 中不使用故事板来设置我的项目。
我的 appDelegate:
.h
#import <UIKit/UIKit.h>
#import "ViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
.m
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
ViewController *vc = [[ViewController alloc] init];
self.window.rootViewController = vc;
return YES;
}
etc...
我的 viewController 文件:
.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
view.backgroundColor = [UIColor redColor];
[self.view addSubview:view];
}
我认为我的代码是正确的,当我 运行 它时我们应该有一个红屏,但我只得到一个黑屏。有人可以告诉我我是否忘记了什么,或者与项目设置有关。谢谢
添加
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
给你的application:didFinishLaunchingWithOptions:
您缺少两个步骤。
初始化window:
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
使 window 键可见:
[self.window makeKeyAndVisible];
总而言之,您的代码应如下所示:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Initialize the window
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
// Add the view controller to the window
ViewController *vc = [[ViewController alloc] init];
self.window.rootViewController = vc;
// Make window key & visible
[self.window makeKeyAndVisible];
return YES;
}
我试图在 xcode 和 objective c 中不使用故事板来设置我的项目。
我的 appDelegate:
.h
#import <UIKit/UIKit.h>
#import "ViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
.m
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
ViewController *vc = [[ViewController alloc] init];
self.window.rootViewController = vc;
return YES;
}
etc...
我的 viewController 文件:
.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
view.backgroundColor = [UIColor redColor];
[self.view addSubview:view];
}
我认为我的代码是正确的,当我 运行 它时我们应该有一个红屏,但我只得到一个黑屏。有人可以告诉我我是否忘记了什么,或者与项目设置有关。谢谢
添加
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
给你的application:didFinishLaunchingWithOptions:
您缺少两个步骤。
初始化window:
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
使 window 键可见:
[self.window makeKeyAndVisible];
总而言之,您的代码应如下所示:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Initialize the window
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
// Add the view controller to the window
ViewController *vc = [[ViewController alloc] init];
self.window.rootViewController = vc;
// Make window key & visible
[self.window makeKeyAndVisible];
return YES;
}