我是iOS开发中的新手,试图学习如何以编程方式创建和设置视图。
我想在Obj-C中做快速陈述
window?.rootViewController = UINavigationController(rootViewController : ViewController())
项目:单一视图应用程序。尝试链接默认的Created ViewController.h
根据Krunals答案我更新了代码,但导航控制器未在模拟器中显示
Cmd +单击控制器不会导航到ViewController文件
#import "AppDelegate.h"
#import "ViewController.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIScreen *screen=[[UIScreen alloc]init];
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.makeKeyAndVisible;
ViewController *controller = [[ViewController alloc] init];
window.rootViewController = [[UINavigationController alloc] initWithRootViewController:controller] ;
答案 0 :(得分:0)
在添加(用作导航的根控制器)导航控制器堆栈之前初始化视图控制器ViewController
。
以下是初始化简单视图控制器的示例代码
UIViewController *controller = [[UIViewController alloc] init];
以下是使用storyboard初始化的示例代码
ViewController *controller = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"<ViewController - string identifier of your view controller>"];
以下是使用NIB / Bundle初始化的示例代码
ViewController *controller = [[ViewController alloc] initWithNibName:@"<ViewController - string NIB name>>" bundle:nil];
根据您的代码和以下评论,请仅尝试此代码(从您的应用代理发布中删除其他代码):
// make sure your NIB name is 'ViewController'
ViewController *controller = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
if (controller != nil) {
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController: controller];
self.window.makeKeyAndVisible;
} else {
//print - your view controller is nil
}
答案 1 :(得分:0)
感谢Krunal的详细解答。
感谢dan支持
我发现问题而不是self.window.rootViewController,我输入了window.rootViewController。
设置self.window.rootViewController解决了问题。
我不知道self.window.rootViewController和window.rootViewController之间的区别以及问题的原因。
如果有人知道答案,请提供评论答案
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UIScreen *screen=[[UIScreen alloc]init];
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.makeKeyAndVisible;
ViewController *controller = [[ViewController alloc] init];
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:controller] ;
答案 2 :(得分:0)
在您执行此操作之前,删除文件Main.storyboard
并让Main interface
选项为空。
并添加此代码:
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
ViewController *vc = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
更新:如果您想将故事板与UINavigationController
一起使用,请尝试以下操作:
答案 3 :(得分:0)
添加UIViewController并添加UINavigationController
UIStoryboard *storyboard = self.window.rootViewController.storyboard;
UIViewController *rootViewController= [storyboard instantiateViewControllerWithIdentifier:kIdentifier];
[self setRootViewController:rootViewController];
#pragma mark - Set RootView Controller
-(void)setRootViewController:(UIViewController *)rootViewController {
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
}
UIStoryboard *storyboard = self.window.rootViewController.storyboard;
UIViewController *rootViewController= [storyboard instantiateViewControllerWithIdentifier:kIdentifier];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[self setRootViewController:navController];
-(void)setRootViewController:(UINavigationController *)rootViewController {
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
}