【问题标题】:How do I initiate a view control from AppDelegate?如何从 AppDelegate 启动视图控件?
【发布时间】:2013-11-22 17:04:02
【问题描述】:

信息

我正在使用 Storyboard 添加视图。在我的故事板中,我有 1 个视图控制器和 1 个标签栏控制器。

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

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

我的标签栏控制器的情节提要 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(我不熟悉)更容易使用。

【问题讨论】:

  • 顺便说一句,您的问题的解决方案可能最好在默认初始 viewControllerviewDidLoad 方法中处理,特别是如果您打算在显示 1-time 后返回此问题,第一次的场景。
  • 如果用户之前没有使用过该程序,那么初始视图控制器将是注册页面,那么代码不应该在 AppDelegate 中吗?
  • 您声明要向第一次使用的用户显示标签栏控制器,但您的代码正试图向现有用户显示它。你能澄清哪一个是正确的吗?
  • 哦,我的坏蒂莫西穆斯,让我澄清一下(我没有注意到我的打字错误)。我希望标签栏控制器显示给现有用户,视图控制器只显示给新用户(因此代码)。

标签: ios cocoa-touch uiviewcontroller storyboard


【解决方案1】:

我认为与其展示标签栏控制器

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

您应该将其设置为根视图控制器(因为您永远不会返回到初始视图控制器)。

self.window.rootViewController = TabBarController;

【讨论】:

  • 蒂莫西,标签栏控制器的呈现是错误出现的地方;但是,通过输入您的第二段代码来替换错误上升的代码段标签栏控制器的呈现,它似乎在构建和调试时工作得很好,没有任何警告和错误。我唯一的问题是;通过将此代码输入到 AppDelegate 中,这个代码在正确性方面是否可以接受?还是将其放入 AppDelete 的这种思维过程完全不正确,还有更好的方法吗?
  • 我认为更好的方法是将标签栏控制器设置为情节提要中的初始控制器,然后以模态方式向首次使用的用户展示注册表单。注册后,您自然会通过关闭注册表单返回标签栏控制器。你现在这样做的方式,不清楚你是如何回到标签栏控制器的?您可能需要在标签栏控制器的viewDidLoadviewWillAppear 中显示模态表单(不确定是哪个)。在应用程序委托中执行此操作可能为时过早。如果您显示错误,可能会说更多。
  • 这段代码的错误 [self presentViewController:TabBarController animated:YES completion:nil]; is No visible @interface for 'AppDelegate' 声明选择器 'presentViewController:animated:completion:' 但是,我完全同意如何将标签栏控制器设置为初始控制器。
【解决方案2】:

更好的方法是在两种情况下都加载 tabbarcontroller,检查用户是否第一次在 tabbarcontroller 的第一个视图控制器中运行应用程序,然后从内部呈现视图控制器

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-11
    • 2015-05-23
    • 1970-01-01
    • 1970-01-01
    • 2014-11-11
    • 1970-01-01
    相关资源
    最近更新 更多