以编程方式设置根viewController

时间:2014-03-15 08:16:52

标签: ios iphone objective-c

我正在学习一些教程。现在我正在努力开发一本电子书。我下载了这个EPUB阅读器库https://github.com/79144876/EpubPDFReader .as我曾经使用过最新的XCODE所以在每个应用程序中我都使用了故事板而不是xib。在这个库中使用了xib。我很难根据我的要求定制库。该库正在做的是当应用程序启动时显示具有四行的表视图1.PDF 2.EPUB等,并且在点击下面的行书时将显示。我想要做的不是显示表视图,而是想在开始时直接显示EpubViewController。 希望你能理解这个问题。我在这里发布一些代码

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSArray *languages = [userDefaults objectForKey:@"AppleLanguages"];


    // Override point for customization after application launch.
    // Add the navigation controller's view to the window and display.


    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];
    return YES;
}

RootViewController.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSError *error;
    NSString *phrase = nil;

    switch (indexPath.row) {
        //TXT
        case 0:{
            //text

        }
            break;
        //PDF
        case 1:{

        }
            break;
        //EPUB
        case 2:{
            epubView = [[EPubViewController alloc] init];
            [epubView loadEpub:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"The Chessmen of Mars" ofType:@"epub"]]];
            epubView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
            [self presentModalViewController:epubView animated:YES];
            [epubView release];
        }
            break;

当案例2调整完整文件时,我不想加载控制器。我想在应用程序启动时加载此控制器。希望你明白这一点

图片

enter image description here enter image description here

4 个答案:

答案 0 :(得分:0)

在AppDelegate中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSArray *languages = [userDefaults objectForKey:@"AppleLanguages"];


    EPubViewController *epubView = [[EPubViewController alloc] init];
    [epubView loadEpub:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"The Chessmen of Mars" ofType:@"epub"]]];
    self.navigationController = [UINavigationController alloc]initWithRootViewController:epubView];
    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];
    return YES;
}

答案 1 :(得分:0)

在AppDelegate文件的.h中创建属性

@property (nonatomic,retain) UINavigationController *navigationController;
@property (nonatomic, strong) EpubViewController *epubcontroller;

在.m AppDelegate文件中写下didFinishLaunchingWithOptions中的代码

epubcontroller=[[EpubViewController alloc] initWithNibName:@"EpubViewController" bundle:nil];
navigationController=[[UINavigationController alloc] initWithRootViewController:epubcontroller];
self.window.rootViewController = self.navigationController;

答案 2 :(得分:0)

在appDelegate.m文件的didFinishLaunchingWithOptions方法中写

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;

确保使用xib创建UIViewController类型的ViewController类。

答案 3 :(得分:0)

从iOS 13开始...

window属性在AppDelegate中不可用。

您需要使用SceneDelegate

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

  var window: UIWindow?

  func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

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

    window = UIWindow(windowScene: windowScene)

    //Here I'm setting LoginViewController as my root view controller
    let storyboard = UIStoryboard(name: "Main", bundle: nil)

    guard let rootVC = storyboard?.instantiateViewController(identifier: "LoginViewController") as? LoginViewController else {
        print("ViewController not found")
        return
     }

     let nav = UINavigationController(rootViewController: rootVC)
     window?.rootViewController = nav
     window?.makeKeyAndVisible()
  }
}