2012-02-16 7 views
0

私はタブコントローラーを使用するアプリケーションを持っています。しかし、タブコントローラーをロードする前に、アプリケーションで使用するためにユーザーの電話番号を保存する必要があるため、ユーザーが電話番号を最初に入力できる画面を入力する必要があります。私はこのようにアプリケーションに番号を保存します。彼らは最初のアプリケーションを入力するときiPhoneリフレッシュAppDelegate

[[NSUserDefaults standardUserDefaults] setInteger:1234 forKey:@"PhoneNumber"]; 

基本的には、ウィンドウはそれらをテキストフィールドにその番号を入力し、それがNSuserDefaultsを使用して保存保存することができますどの開きます。 appDelegateで

、私は値が格納されているかどうかを確認またはこの

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    NSInteger phoneNumber=[[NSUserDefaults standardUserDefaults] integerForKey:@"PhoneNumber"]; 

    if(phoneNumber == 0){ 
     //So this is the window which allow them to enter their phone number 
     UIViewController *phoneNumberVC = [[PhoneNumberVC alloc] initWithNibName:@"PhoneNumber" bundle:nil]; 
     UIViewController *phoneNumberRootVC = [[UINavigationController alloc] initWithRootViewController:phoneNumberVC]; 
     self.window.rootViewController = phoneNumberRootVC;  
     [self.window makeKeyAndVisible];  
    }else{ 
     //Code here sets up tab controller and all the tabs etc 
    } 
} 

今、コード番号が保存された後、私が持っているという事実を除いて、正常に動作を好まないためにif文を持っていますシミュレータを終了して再度実行すると、タブ画面が表示されます。しかし、ユーザーが電話番号画面のボタンを押して番号を保存するとすぐに、タブコントローラーにすぐにロードして欲しい。電話番号画面の現在の方法はこれです:

-(IBAction)testing{ 
    NSString *st = [phoneTF text]; 
    NSInteger pn = [st intValue]; 
    NSString *display = [NSString stringWithFormat:@"%i", pn]; 
    [[NSUserDefaults standardUserDefaults] setInteger:pn forKey:@"PhoneNumber"]; 
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Warning" 
                message:display 
                delegate:self 
              cancelButtonTitle:@"Okay" 
              otherButtonTitles:nil]; 
    [alert show]; 
} 

アラートがちょうどテストの目的です。したがって、ボタンを押した後、タブコントローラーをアプリケーションデリゲートにロードします。とにかく、アプリケーションやアプリケーションの代理人を更新することはできますか?

誰かが私を正しい方向に向けることができたら非常に感謝します!!!

EDIT

Application tried to push a nil view controller on target <UINavigationController: 0x8056d40>. 

[self.navigationController pushViewController:app.tabBarController animated:YES]; 

を試みたが、tabBarControllerがまだ初期化されていないとして、それは

コンソールのエラーが実行されませんので、私は実行する必要がAppDelegateをもう一度実行すると、タブコントローラとすべてのビューが初期化されます

答えて

1

したい、他のビューに切り替えるには、ナビゲーションコントローラを要求するコードと

UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:display 
              delegate:self cancelButtonTitle:@"Okay" otherButtonTitles: nil]; 
[alert show]; 

を置き換える:

YourOtherViewController *yourOtherViewController = [[YourOtherViewController alloc] init]; 
[self.navigationController pushViewController:yourOtherViewController animated:YES]; 

編集

あなたからAppDelegateにアクセスする場合phoneNumberVCの場合は、次のような操作を行うことができます:

if(phoneNumber == 0){ 
    UIViewController *phoneNumberVC = [[PhoneNumberVC alloc] initWithNibName:@"PhoneNumber" bundle:nil]; 
    phoneNumberVC.appDelegate = self; // add this property to PhoneNumberVC 
    UIViewController *phoneNumberRootVC = [[UINavigationController alloc] initWithRootViewController:phoneNumberVC]; 
    self.window.rootViewController = phoneNumberRootVC; 
} else { 
    [self setUpTabViewController]; // add this function 
} 
[self.window makeKeyAndVisible]; 

そしてPhoneNumberVCのような何か:あなただけの新しいXIBを開きたいが、私はタブコントローラを開く必要がある場合、コードは罰金であることを

[self.appDelegate setUpTabViewController]; 
+0

。タブコントローラとすべてのタブ画面は、すべてPhoneNumberVCではなくAppDelegateに設定されています。だから私の問題は、AppDelegateに再度アクセスしてそこからコードを実行する方法です。電話番号画面はタブコントローラーの一部ではなく、通常の表示です – AdamM

+0

私の更新された回答を参照してください。それはあなたのために働くのですか? – sch

+0

残念ながら。 Appデリゲートのifステートメントは、ユーザーが電話番号画面に番号を保存するまで、タブコントローラーが初期化されないことを意味します。それが終わったら、もう一度、アプリケーションデリゲートコードを実行して、タブナビゲーションコントローラを設定する必要があります。私の編集を見て、私が試したことを確認してください – AdamM

関連する問題