11

我有一个我的 ApplicationDelegate 实现的协议。我想将此传递给故事板中定义的第一个视图控制器。如何从方法中访问它?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
4

3 回答 3

23

我不确定我是否正确理解了这个问题,我想您是在问如何在情节提要中获得第一个 viewController。为了得到这个你打电话,

UIStoryboard *storybord = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc =[storybord instantiateInitialViewController];

更改故事板的名称以适合您的名称,MainStoryboard 只是默认名称。希望这就是你要找的。

编辑:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIStoryboard *storybord = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    UIViewController *vc =[storybord instantiateInitialViewController];
    //set the delegate on the view controller that you have loaded
    // Override point for customization after application launch.
    return YES;
}
于 2011-11-15T10:20:26.933 回答
14

而不是使用创建现有故事板的新副本

UIStoryboard *storybord = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

您可以在应用程序委托的头文件中设置此属性

@property (nonatomic, weak) UIViewController* initialViewController;

在此方法中,只需将 self.window.rootViewController 分配给该属性

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.initialViewController = self.window.rootViewController;

    return YES;
}

这应该可以工作,因为在应用程序委托协议的窗口属性中可以访问 rootViewConroller,如果使用情节提要,则此控制器是 initialViewController。

于 2013-05-09T08:40:51.063 回答
-2

Swift 2.0 为那些想要的人实现。

您可能需要修改 STROYBAORD 和控制器视图标识符。还有o

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        let storyboard: UIStoryboard = UIStoryboard(name: "[STORYBOARD NAME]", bundle: nil)
        let viewController:UIViewController = storyboard.instantiateViewControllerWithIdentifier("[CONTROLLER IDENTIFIER]")   // You have replace UIViewController with you customer controll type


        self.window?.rootViewController = viewController
        self.window?.makeKeyAndVisible()

        return true
    }
于 2016-04-20T10:22:39.660 回答