ios - 如何从 AppDelegate 启动 View 控件?

标签 ios cocoa-touch uiviewcontroller storyboard

信息

我正在使用 Storyboard 添加 View 。在我的 Storyboard中,我有 1 个 View Controller 和 1 个标签栏 Controller 。

我想要完成的是检查用户是否是第一次使用我的程序,如果是这样,它不会显示标签栏 Controller ,而是显示初始 View Controller 。

这是我的代码,其中包含 [self presentViewController..] 处的 AppDelegate.m 中的错误。

我的标签栏 Controller 的 Storyboard ID 是“TabBarController”。

请注意 ViewController.h 和 ViewController.m 是 UNTOUCHED。

代码

GlobalHeader.h

#ifndef dirt_GlobalHeader_h
#define dirt_GlobalHeader_h

BOOL RegisterCheck;

#endif

AppDelegate.h
#import <UIKit/UIKit.h>
#import "GlobalHeader.h"
#import "ViewController.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

AppDelegate.m
#import "AppDelegate.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    RegisterCheck = [[NSUserDefaults standardUserDefaults] boolForKey:@"RegisterCheck"];

    if (!RegisterCheck)
    {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"RegisterCheck"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    else
    {
        UIStoryboard *MainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UIViewController *TabBarController = [MainStoryBoard instantiateViewControllerWithIdentifier:@"TabBarController"];
        [self presentViewController:TabBarController animated:YES completion:nil];
    }
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

我感谢任何人对我的问题的任何贡献。

我还想说 Visual Studio IDE 比这个 XCODE(我不熟悉)更容易使用。

最佳答案

我认为,而不是呈现标签栏 Controller

[self presentViewController:TabBarController animated:YES completion:nil];

您应该将其设置为 Root View Controller (因为您永远不会返回到初始 View Controller )。
self.window.rootViewController = TabBarController;

关于ios - 如何从 AppDelegate 启动 View 控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19897144/

相关文章:

ios - 使用 Swift 在 iOS 中打印 View

objective-c - 实现协议(protocol)的问题

iphone - 显然我不知道如何写一个setter

ios - 在 UINavigationController 中,topViewController、visibleViewController、presentedViewController 之间有什么区别?

ios - 如何在 UITableviewCell 中单击时更改 UIButton 的背景颜色?

ios - 仅允许从 UITextField 中选择和复制不起作用

ios - 如何隐藏文件夹中的特定文件。

ios - UITableViewController 无声地崩溃而没有错误

iOS Segue - 何时实例化 viewControllers

ios - 如何在其他 UIViewController 中显示 UIViewController?