[英]Setting a subview's view controller programatically with objective-c
[英]How to set Root View Controller programatically in Objective C?
我是 iOS 開發的新手,試圖學習如何以編程方式創建和設置視圖。
我正在嘗試在 Obj-C 中快速聲明
window?.rootViewController = UINavigationController(rootViewController : ViewController())
項目:單視圖應用程序。 嘗試鏈接默認創建的 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] ;
在添加(用作導航的根控制器)到導航控制器堆棧之前初始化您的視圖控制器ViewController
。
這是初始化簡單視圖控制器的示例代碼
UIViewController *controller = [[UIViewController alloc] init];
這是使用故事板初始化的示例代碼
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
}
感謝 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] ;
刪除文件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
一起使用,請嘗試以下操作:
添加 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];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.