問題描述
我剛剛開始使用 iOS 編程,到目前為止,我在這里找到的教程和答案對我前進有很大幫助.然而,這個特殊的問題讓我整晚都在煩惱,我找不到感覺正確"的答案.
I've just started on iOS programming and so far the tutorials and answers I found here have been a great help to move forward. However, this particular problem has been bumming me all night and I can't find an answer that "feels right".
我正在編寫一個連接到遠程服務的應用程序,用戶需要先登錄才能使用它.當他們開始使用應用程序時,他們的第一個視圖應該是登錄對話框;當他們之前進行過身份驗證時,他們會立即看到概覽頁面.
I'm writing an application that connects to a remote service and the users need to sign in before they can use it. When they start using the application, their first view should be the sign in dialog; when they've authenticated before, they immediately see the overview page.
該項目使用故事板——我認為這是一個很棒的特性——所以選擇和加載根視圖控制器的大部分代碼都已經處理好了.我認為添加邏輯的最佳位置是 AppDelegate
:
The project uses story boards - which I think is a great feature - so most of the code that selects and loads the root view controller is already taken care of. I thought the best place to add my logic is the application:didFinishLaunchingWithOptions:
method of the AppDelegate
:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions
{
// select my root view controller here based on credentials present or not
return YES;
}
但這帶來了兩個問題:
在這個特定的委托方法中,根視圖控制器已經根據故事板被選擇(并加載?).我是否可以在加載過程中移到更早的位置以覆蓋第一個視圖控制器選擇,或者這會使事情變得不必要地復雜化?
Inside this particular delegate method, the root view controller has already been selected (and loaded?) based on the story board. Could I move to an earlier spot in the loading process to override the first view controller selection or would that needlessly complicate matters?
要覆蓋第一個視圖控制器,我需要引用故事板,但我找不到比使用 的
.感覺不對,應用程序應該已經有故事板的引用,但是我怎么才能訪問它呢?storyboardWithName:bundle:
構造函數更好的方法UIStoryboard
To override the first view controller I need a reference to the story board, but I couldn't find a better way than to use the storyboardWithName:bundle:
constructor of UIStoryboard
. That feels wrong, the application should already have a reference to the story board, but how can I access it?
更新
我解決了我遇到的第二個問題,因為我在這里找到了答案:
I worked out the second issue I was having, as I found my answer here:
UIStoryboard:獲取活躍的故事板?
NSBundle *bundle = [NSBundle mainBundle];
NSString *sbFile = [bundle objectForInfoDictionaryKey:@"UIMainStoryboardFile"];
UIStoryboard *sb = [UIStoryboard storyboardWithName:sbFile bundle:bundle];
以上將創建一個新的故事板實例;要獲取活動實例,要簡單得多:
The above will create a new story board instance; to get the active instance, it's a whole lot simpler:
UIStoryboard *sb = [[self.window rootViewController] storyboard];
在故事板文件本身中,您必須為要加載的視圖設置標識符,例如登錄對話框
.然后你像這樣實例化視圖:
In the story board file itself you have to set an identifier for the view you wish to load, e.g. LoginDialog
. Afterwards you instantiate the view like this:
LoginViewController *login = [sb instantiateViewControllerWithIdentifier:@"LoginDialog"];
[self.window setRootViewController:login];
在另一個視圖控制器中,以下就足夠了:
Within another view controller, the following suffices:
UIStoryboard *sb = self.storyboard;
LoginViewController *login = [sb instantiateViewControllerWithIdentifier:@"LoginDialog"];
[self presentViewController:login animated:NO completion:nil];
推薦答案
只需重置窗口的根視圖控制器即可
You can just reset the root view controller of the window
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions
{
if(your_condition) {
UIViewController *newRoot = [your implementation];
self.window.rootViewController = newRoot;
}
return YES;
}
這對我有用,Xcode5.0.1
This is worked for me, Xcode5.0.1
這篇關于在應用程序啟動時從情節提要中選擇替代的第一個視圖控制器的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!