Load an other View from applicationDidFinishLaunching in the AppDelegate - iphone

I have this code in my applicationDidFinishLaunching in the AppDelegate:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *firsttime = [defaults stringForKey:#"firsttime"];
if (firsttime == nil) {
BenutzerdatenViewController *Benutzer = [[BenutzerdatenViewController alloc] initWithNibName:nil bundle:nil];
Benutzer.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[Benutzer release];
[defaults setObject:#"lasttime" forKey:#"firsttime"];}
else { [window addSubview:viewController.view];
[window makeKeyAndVisible];}
Always when I open the app the first time I just see a with "view" instead of the right view with buttons and so on. Where is the problem?

You need to add the actual view to your window. For firsttime, you need the following:
[window addSubview: Benutzer.view];
Also, don't release that viewController; you should store a reference to it somewhere.
In the other case (!firstime), it's unclear where you're getting viewController from; I assume it's a member variable.

Related

How to Redirect to home tab after login Iphone App?

In my AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
//Get uer ID from user defaults
NSString *userid = [defaults objectForKey:#"UserId"];
if([userid isEqualToString:#""]){
login = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
[window addSubview:login.view];
} else {
[window addSubview:[rootTabBarController view]];
}
[self.window makeKeyAndVisible];
return YES;
}
and after login success i have this code
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:serverOutput forKey:#"UserId"];
//show tabbar app
NewClassMoonAppDelegate * appsDelegate =[[UIApplication sharedApplication] delegate];
[appsDelegate.window addSubview:[appsDelegate.rootTabBarController view]];
so the question is how can i redirect to specific tab [Home tab FOR EXAMBLE]?
[appDelegate.rootTabBarController setSelectedIndex:GiveIndexOfTab]; // give index of home tab for ex. 0
If you want to jump to a specific tab without intercepting the tabBar, rather than the first default tab
use
[appsDelegate.rootTabBarController setSelectedIndex:2]; // 2 is a pseudo index i have assigned
For tabbar controller,
SelectedIndex propery helps you to manage its behavior.
Use
setSelectedIndex:Index
method. It'll solve your purpose.
Thanks.

Problems displaying view controller when local notification received on iPhone

The following code is my latest attempt to display a view controller when my application receives a particular local notification:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
NSLog(#"Notification Received. UserInfo: %#", [notification.userInfo valueForKey:#"notificationType"]);
if ([[notification.userInfo valueForKey:#"notificationType"] isEqualToString:#"backup"])
{
NSLog(#"Backup notification received.");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:#"Yes" forKey:#"shouldBackup"];
[defaults synchronize];
SettingsViewController *vc = [self.window.rootViewController.tabBarController.viewControllers objectAtIndex:3];
[self.window.rootViewController.tabBarController.navigationController pushViewController:vc animated:NO];
}
else
{
NSLog(#"Did receive notification: %#, set for date:%# .", notification.alertBody, notification.fireDate);
}
}
I then have this piece of code which I use in the viewDidAppear method to determine wether or not to perform the backup:
if ([[defaults objectForKey:#"shouldBackup"] isEqualToString:#"Yes"]){
[defaults setObject:#"No" forKey:#"shouldBackup"];
[defaults synchronize];
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
HUD.delegate = self;
HUD.labelText = #"Backing Up Your Data..";
HUD.minSize = CGSizeMake(135.f, 135.f);
[HUD showWhileExecuting:#selector(performBackup) onTarget:self withObject:nil animated:YES];
}
However, it appears that viewDidAppear is never called, can anyone explain what it is I'm doing wrong? I've tried several different approaches now and I just can't seem to get it to work..
Thanks,
Tysin
You need to push the SettingsViewController onto the stack from a NavigationController in order for its delegate methods to be called (in this case viewDidAppear). It seems that your root view controller is a TabBarController. In this case add the UINavigationControllerDelegate to your root VC, more info on what to do next can be found here http://www.touchthatfruit.com/viewwillappear-and-viewdidappear-not-being-ca
Also, are you sure viewDidAppear is not getting called or just the code within it is not getting called? Add a breakpoint to find out.
Lastly, do you haveĀ [super viewDidAppear:animated]; in the viewDidAppear method of SettingsViewController?
If all else fails you can always call viewDidAppear manually from the parent view controller :)
Hope this helps!

Why can't I launch this modal view from didFinishLaunchingWithOptions?

I am trying to do something pretty easy, in my estimation:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
prefs = [NSUserDefaults standardUserDefaults];
BOOL IsLoggedIn = [prefs boolForKey:#"IsLoggedIn"];
if(IsLoggedIn == NO)
{
//Show login controller
LoginViewController *lvc = [[LoginViewController alloc] initWithNibName:nil bundle:nil];
[self.tabBarController presentModalViewController:lvc animated:NO];
[lvc release];
}
else if(IsLoggedIn == YES)
{
//Continue doing crap
}
// Override point for customization after application launch.
// Add the tab bar controller's current view as a subview of the window
self.window.rootViewController = self.tabBarController;
NSArray *tabs = self.tabBarController.viewControllers;
UIViewController *tbInvoice = [tabs objectAtIndex:0];
tbInvoice.tabBarItem.image = [UIImage imageNamed:#"Open-Mail.png"];
UIViewController *tbClient = [tabs objectAtIndex:1];
tbClient.tabBarItem.image = [UIImage imageNamed:#"Breifcase.png"];
[self.window makeKeyAndVisible];
return YES;
}
When using the debugger, I see it enter if(IsLoggedIn == NO) and run the LoginViewController code, but the view never shows.
It's driving me crazy.
I tried running the code after [self.windoow makeKeyAndVisible], but it didn't change anything.
This code looks like every example I've seen. Can anyone see what I'm doing wrong?
Thanks in advance,
Clif
I came up with this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//...
if(!loggedIn)
{
// Launch the app with login controller as the rootController
self.window.rootViewController = loginController;
// ...but switch to the original controller as soon as the UI is presented
dispatch_async(dispatch_get_main_queue(), ^{
self.window.rootViewController = originalRootController;
// ...and silently present the login controller again with no noticeable changes
[originalRootController presentViewController:loginController
animated:NO
completion:NULL];
});
}
Hpoe this post will give you some idea.

Can I call presentModalViewController in viewDidLoad..?

In my iPhone app I have HomeViewController and ContentViewController. I am saving the values in ContentViewController by using NSUserDefaults
and based on saved values I will load the ContentView instead of HomeView when the app is restarted. if there r no values in the NSUserDefaults it displays the HomeView.
in HomeView I have some buttons..it's like this.. each button is for a book so in contentView all the page nos (in the bottom in a scroll view in ContentView) will be displayed if I click on a page no it displays the text in the above label of ContentView.if the user closes the app in contentView, the page no and book no will be saved...if the user clicks on home button all the information will be deleted.
In the Homeview im checking the NSUserDefaults, if it contains values it should display that exact page of that book
the following is the code...
//HomeViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
contentViewController = [[ContentViewController alloc] initWithNibName:#"ContentView" bundle:nil];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSLog(#"...%d,%d,%d",[prefs integerForKey:#"Stage"],[prefs integerForKey:#"Stimulus"],[prefs integerForKey:#"Card"]);
if(!([prefs integerForKey:#"Stage"] ==0 && [prefs integerForKey:#"Stimulus"] ==0 && [prefs integerForKey:#"Card"] ==0)){
[contentViewController setCurrentState:[prefs integerForKey:#"Stage"]];
[contentViewController setCurrentStimulus:[prefs integerForKey:#"Stimulus"]];
[contentViewController setCurrentCard:[prefs integerForKey:#"Card"]];
[self presentModalViewController:contentViewController animated:YES];
}
}
but it's displaying the homeview.
Try using the method viewDidAppear shown below instead of viewDidLoad
- (void)viewDidAppear:(BOOL)animated
{
contentViewController = [[ContentViewController alloc] initWithNibName:#"ContentView" bundle:nil];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSLog(#"...%d,%d,%d",[prefs integerForKey:#"Stage"],[prefs integerForKey:#"Stimulus"], [prefs integerForKey:#"Card"]);
if(!([prefs integerForKey:#"Stage"] ==0 && [prefs integerForKey:#"Stimulus"] ==0 && [prefs integerForKey:#"Card"] ==0))
{
[contentViewController setCurrentState:[prefs integerForKey:#"Stage"]];
[contentViewController setCurrentStimulus:[prefs integerForKey:#"Stimulus"]];
[contentViewController setCurrentCard:[prefs integerForKey:#"Card"]];
[self presentModalViewController:contentViewController animated:YES];
}
}

NSuserdefaults not loading in a tab bar controller, works on uiview

I have a problem getting the NSUserDefaults to pull out on a tab/nav/tableview controller page, I've put the same code on a loginpage and the app will pull the objects for keys perfectly. I had this working when I had programmatically created the tabs and navs but now it's not working. The loginViewController is not in the tab stack, is that the reason it works?
-(void)refreshFields {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
usernameLabel.text = [defaults objectForKey:kUsernameKey];
passwordLabel.text = [defaults objectForKey:kPasswordKey];
}
- (void)viewDidAppear:(BOOL)animated {
[self refreshFields];
[super viewDidAppear:animated];
if ([usernameLabel.text length] == 0|| [passwordLabel.text length] == 0)
{
UINavigationController *loginNavigationController = [[UINavigationController alloc] initWithRootViewController:loginViewController];
[loginViewController release];
[self.navigationController presentModalViewController:loginNavigationController animated:NO];
[loginNavigationController release];
}
else
{
[[self tableView ]reloadData];
}
}
Your usernameLabel and passwordLabel outlets aren't set. Verify by putting a breakpoint in refreshFields.