我有一個我的ApplicationDelegate實現的協議。我想把它傳遞給故事板中定義的第一個視圖控制器。如何從該方法訪問它?UIStoryBoard從ApplicationDelegate獲取第一個視圖控制器
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
我有一個我的ApplicationDelegate實現的協議。我想把它傳遞給故事板中定義的第一個視圖控制器。如何從該方法訪問它?UIStoryBoard從ApplicationDelegate獲取第一個視圖控制器
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
我不知道如果我理解正確的問題,我認爲你是問你如何讓在故事板第一的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;
}
而是然後用
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如果用故事板。
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
}
是的,這就是我所需要的。關於如何在通用應用程序(iphone vs ipad)中加載設備的正確故事板的任何想法? –
這應該這樣做 'UIViewController * vc;如果(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPad){UIStoryboard * storybord = [UIStoryboard storyboardWithName:@「MainiPadStoryboard」bundle:nil]; vc = [storybord instantiateInitialViewController]; } else { UIStoryboard * storybord = [UIStoryboard storyboardWithName:@「MainiPhoneStoryboard」bundle:nil]; vc = [storybord instantiateInitialViewController]; }' –
很確定這是抓取一個新的副本,而不是現有的副本。 –