[英]How to set rootViewController?
我正在嘗試學習這本書,但所有示例都沒有 storyBoard,當我嘗試構建應用程序時,我發現了這個錯誤:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Application windows are expected to have a root view controller at the end of application launch'
我試圖用這個修復它:
ViewController *myView = [[ViewController alloc] init];
[self.window addSubview:myView];
self.window.rootViewController = myView;
但后來我收到了另一個錯誤:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[HypnoView _preferredInterfaceOrientationGivenCurrentOrientation:]: unrecognized selector sent to instance 0x78f68730'
有人可以向我解釋為什么會發生在我身上嗎?
首先你不能添加
UIViewController
作為子視圖
方法
addSubview:(UIView *)
需要一個 UIview,而您正在添加一個 UIViewController。
其次,在 iOS 4 及更早版本中使用了添加子視圖,現在您添加一個視圖控制器作為 rootviewcontroller。
所以簡單地添加,
self.window.rootViewController = myView
如果你想使用 addSubview,你需要這樣做
[self.window addSubView:myView.view]
轉到 AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window.rootViewController = self.viewController;
return YES;
}
它是這樣做的。 轉到 AppDelegate.m 並轉到 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
ViewController *myView = [[ViewController alloc] init];
self.window.rootViewController = myView;
[self.window makeKeyAndVisible];
如果要將 UIView 添加為子視圖。 然后執行以下操作:
UIView *view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window makeKeyAndVisible];
[self.window addSubview:view];
目標-c
@interface AppDelegate ()
@property (nonatomic, strong) ViewController *viewController;
@end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self setViewController:[[ViewController alloc] init]];
[self setWindow:[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]];
[[self window] setBackgroundColor:[UIColor redColor];
[[self window] setRootViewController:[self viewController]];
[[self window] makeKeyAndVisible];
return YES;
}
迅速
var window: UIWindow?
let viewController = ViewController();
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window!.backgroundColor = UIColor.redColor()
self.window!.rootViewController = playerController
self.window!.makeKeyAndVisible()
return true
}
試試這個代碼
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
ViewController *controller = [[ViewController alloc]initWithNibName:@"View" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:controller];
[self.window setRootViewController:nav];
[self.window makeKeyAndVisible];
return YES;
}
您需要設置:-
在 AppDelegate.m 文件中: _applicationDidFinishLaunchingWithOptions_
UIView *myView;
self.window.rootViewController = myView
myView= self.viewController;
[self.window makeKeyAndVisible];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.