在没有故事板的情况下启动 ios 项目

问题描述 投票:0回答:10

我在使用 xibs 而不是故事板启动 iOS 应用程序时遇到了一些麻烦。问题是我得到一个黑屏并且没有调用第一个视图控制器(为

viewDidLoad
方法添加了断点)。

在应用程序委托标头中我声明了这一点:

@property (strong, nonatomic) UIWindow window;
@property (strong, nonatomic) ViewController *viewController;

didFinishLaunchingWithOptions
方法中我有这个实现:

self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
navController.navigationBarHidden = YES;

self.window.rootViewController = navController;
[self.window makeKeyAndVisible];

查看一些论坛我发现我应该分配窗口所以我将其添加为函数的第一行

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

问题是,当我这样做时,应用程序在从

didFinishLaunchingWithOptions
方法返回后崩溃(没有任何痕迹的 SIGABRT)。

我还尝试使

navController
成为一个属性,并实例化一个默认的
UIViewController
类初始化相同的 xib

我做错了什么?

感谢和问候

ios objective-c xib
10个回答
8
投票

希望对你有帮助:

删除view controller和storyboard文件,新建viewController.h,viewController.h.m,viewController.xib文件。

 #import "AppDelegate.h"

 @interface AppDelegate ()

 @end

 @implementation AppDelegate
 @synthesize viewCOntrollerobj;

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
     self.viewCOntrollerobj = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
     UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewCOntrollerobj];
     //navController.navigationBarHidden = YES;

     self.window.rootViewController = navController;
     [self.window makeKeyAndVisible];
     return YES;

}  


2
投票

要更改您的项目以使用 xib 而不是故事板,请首先为每个视图控制器创建一个 xib。您需要将文件的所有者更改为要为其创建 xib 的视图控制器的类。然后将 File's Owner view outlet 链接到 xib 中的视图。

之后,选择您的应用程序目标并将主界面下拉菜单更改为空。现在你可以删除故事板文件了。

最后,在应用委托的

application:didFinishLaunchingWithOptions:
方法中初始化您的窗口,并将您的初始视图控制器设置为窗口的根视图控制器。然后在您的应用程序委托窗口上调用
makeKeyAndVisible
,您应该可以开始了。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [UIWindow new];
    self.window.rootViewController = [ViewController new];
    [self.window makeKeyAndVisible];
    return YES;
}

2
投票

在 xCode 11 中:

第一步:

-> 删除故事板文件

第二步:

在yourProject > General > deployment info > main interface -> delete reference to storyboard

第三步:

在info.plist > Application Scene Manifest > Scene Configuration > Application Session Role > item 0 > storyboard name

->删除行

第四步:

In SceneDelegate.m

 - (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {

          self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
          self.window.windowScene = (UIWindowScene *)scene;
          self.window.rootViewController = [[UINavigationController alloc] 
                               initWithRootViewController:ViewController.new];
          [self.window makeKeyAndVisible];

}  

确保像这样导入您的 viewController 头文件:#import "ViewController.h"


1
投票

OK,终于明白了

我所要做的就是再次添加

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

在此之后,只需删除.h、.m 和.xib 并重新创建即可。

出于任何原因,它现在工作正常。


0
投票

这是一种“开箱即用”的解决方案,因此与您的代码无关。特别是因为我没有发现您的代码有任何问题。

我遇到了同样的问题并尝试了很长时间来修复代码,对我有用的是找到一个使用 xibs 的示例项目(例如在 github 中)。

下载并编辑它以确保您的应用程序正常工作。很遗憾 Xcode 想要强迫我们使用他们的故事板来解决这些问题。


0
投票

我没有尝试使用 XIB,但这东西在这里工作正常:::

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.viewController = [[ViewController alloc] init];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
    navController.navigationBarHidden = YES;

    self.window.rootViewController = navController;
    [self.window makeKeyAndVisible];
    // Override point for customization after application launch.
    return YES;
}

在 ViewController 的 viewDidLoad

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor greenColor]];
    // Do any additional setup after loading the view, typically from a nib.
}

绿色出现在屏幕上。

在为窗口设置根视图控制器之前初始化窗口。

这是我在你的代码中看到的唯一问题,因为 rootViewController 被设置为一个零窗口,然后你正在初始化。


0
投票
  1. 留空主界面

  2. 在项目中添加新的 ViewController.xib 并将其文件的所有者类标记为“ViewController”

  3. 在 AppDelegate.m 中

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    NSArray* xibContents = [[NSBundle mainBundle] loadNibNamed:@"ViewController"
                                                         owner:nil
                                                       options:nil];
    ViewController* vc = [xibContents objectAtIndex:0];
    UINavigationController *navigationController =
        [[UINavigationController alloc] initWithRootViewController:vc];
    self.window.rootViewController = navigationController;
    [self.window makeKeyAndVisible];
    
    return YES;
    

    }

  4. 构建并运行


0
投票

在 Swift 3.0 中

//MARK: Initial View Controller
func initialViewController(){
    self.window = UIWindow(frame: UIScreen.main.bounds)
    let rootViewController = UIViewController(nibName: "HomeVC", bundle: nil)
    let navigation : UINavigationController = UINavigationController(rootViewController: rootViewController)
    navigation.isNavigationBarHidden = true
    self.window?.rootViewController = navigation
    self.window?.makeKeyAndVisible()
}

在 appdelegate.swift 中

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    initialViewController()

    return true
}

0
投票

带有 SceneDelegate 的 Xcode 11

In SceneDelegate.m

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.windowScene = (UIWindowScene *)scene;
self.viewController = [[ViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
}

无需在 AppDelegate.m 中做任何事情


0
投票

iOS 16、Xcode14解决方案:

  1. 从项目导航器中删除 Main.storyboard
  2. 从 Info.plist 中删除故事板名称 Info.plist -> 信息属性列表 -> 应用场景清单 -> 场景配置 -> 应用会话角色 -> 项目 0
  3. 从 Info.plist Values Target -> Build Settings -> Info.plist Values 中删除“UIKit Main Storyboard File Base Name”
  4. 在 SceneDelegate.swift 中添加代码 SceneDelegate -> scene(_:willConnectTo:options:).

guard let windowScene = (scene as? UIWindowScene) else { return }

window = UIWindow(frame: windowScene.coordinateSpace.bounds)
window?.windowScene = windowScene
window?.rootViewController = ViewController() // Your main VC
window?.makeKeyAndVisible()

© www.soinside.com 2019 - 2024. All rights reserved.