Ok I have an application that uses tab controller. However before I want the tab controller to load up, I want the user to enter a screen which allows them to enter their phone number first as I need to save the users phone number for use in the application. I save the number to the application like this.
[[NSUserDefaults standardUserDefaults] setInteger:1234 forKey:@"PhoneNumber"];
Basically when they first enter application, a window will open which will allow them to store enter their number in text field and it saved using the NSuserDefaults.
In the appDelegate, I have an if statement to check if a value is stored or not like this
- (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
}
}
Now the code works fine, except for the fact that after the number is saved, I have to exit the simulator, and run it again, and then the tab screens appear. However, I want the tab controller to load up a soon as the user has pressed the button in the phone number screen to save the number. Current method in the phone number screen is this:
-(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];
}
The alert is just for testing purposes. So after the button is pressed, I want the tab controller to load up in the app delegate. Is there anyway I can refresh the application or the app delegate?
Would be very grateful if someone could point me in the right direction!!!
EDIT
Tried
[self.navigationController pushViewController:app.tabBarController animated:YES];
But as the tabBarController is not initialised yet, it won’t run
Console error
Application tried to push a nil view controller on target <UINavigationController: 0x8056d40>.
So I need to run the AppDelegate again, so the tab controller and all its views will be initialised
replace
with code that asks the navigation controller to switch to the other view you want:
Edit
If you want to access the AppDelegate from
phoneNumberVC, you can do something like the following:And in PhoneNumberVC something like: