setBadgeValue not working in AppDelegate.m (Storyboard) - ios

In my AppDelegate didFinishLaunchingWithOptions BOOL I am calling this method:
UIStoryboard *iPhone35Storyboard = [UIStoryboard storyboardWithName:#"Storyboard3Inch" bundle:nil];
UITabBarController *tabBarController = [iPhone35Storyboard instantiateViewControllerWithIdentifier:#"tabBarController"];
[[tabBarController.tabBar.items objectAtIndex:1] setBadgeValue:#"2"];
I thought that now, during the App Launch, a badge would be set.
But no badge is displayed!
Storyboard ID is set # the TabBarViewController.
And I also tried instantiateInitialViewController instead of instantiateViewControllerWithIdentifier:#"tabBarController".
Can someone help?

Related

Can't alter UITabBarController badge in App Delegate when Tab Bar isn't root view controller

I would like to display a badge value on my tab bar everytime the below method gets called. The NSLog appears in the console, so the method works properly, but somehow the tab bar badge doesn't appears anyway. Am I doing it wrong? Or this part should be good and I missed something elsewhere?
// AppDelegate.m
- (void)pubnubClient:(PubNub *)client didReceiveMessage:(PNMessage *)message {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UINavigationController *tabBarController = [storyboard instantiateViewControllerWithIdentifier:#"tab"];
UITabBarController *tabController = (UITabBarController *)tabBarController;
[[tabController.viewControllers objectAtIndex:0] tabBarItem].badgeValue = #"1";
NSLog(#"SHOW BADGE");
}
UINavigationController *tabBarController = [storyboard instantiateViewControllerWithIdentifier:#"tab"];
UITabBarController *tabController = (UITabBarController *)tabBarController;
This line (aside from the strange multiple casting) instantiates a new tab bar controller, it does not return a reference to the existing one. You should keep a reference to the existing tab bar controller instead of creating a new one.
Depending on your setup, you may be able to use the following instead:
UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;

In didFinishLaunchingWithOptions method, I can not set index of tabbar

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
if (condition) {
UITabBarController *tabbar = (UITabBarController *)self.window.rootViewController;
[tabbar setSelectedIndex:4];
}
...
}
In didFinishLaunchingWithOptions method, I can not set selected index of tabbar.
UITabbarController is initial viewController in my project.
I want to set another tabbar index for special condition, when users launch my app.
I have tested ios7, ios8.
Is there any other way?
I think that the problem in your code, is that there's no identified UITabBarController... If you're using storyboard it's easy to make this connection -
UIStoryboard *storyboard = self.window.rootViewController.storyboard;
UITabBarController *tabBarViewController = (UITabBarController *)[storyboard instantiateViewControllerWithIdentifier:#"tabbar"]; //Set this identifier in the storyboard
[tabBarViewController setSelectedIndex:1];

Loading new Storyboard and UIButton inactive

I added a new UIViewController to get a log in issue fixed and now the button in the NavigationBar on the next scene are inactive. Here is the code I used to load the new StoryBoard:
-(void)newSotrybooard{
UIStoryboard *settingsStoryboard = [UIStoryboard storyboardWithName:#"LogedIn" bundle:nil];
UIViewController *initialSettingsVC = [settingsStoryboard instantiateInitialViewController];
initialSettingsVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:initialSettingsVC animated:YES completion:NULL];
}
I added the new storyboard after I couldn't get the NavigationBar to show up and push without and error on the main storyboard. This is the only code that has changed. The Navigation Controller does not have a class associated with it and neither does the TableView to follow. I can also not scroll the Table view. Thank you for your help!
Update:
I updated the code above and
The error I receive is:
Warning: Attempt to present <UINavigationController: 0x109fa0ce0> on <PrivateViewController: 0x109f6ef80> whose view is not in the window hierarchy!
Based on the information you have provided, I surmise that the following could be helpful -
Yes, you have used instantiateViewControllerWithIdentifier.
But, remember it instantiates and returns the view controller with
the specified identifier. You're missing that.
Provide the same name to your new Storyboard in your code and
File Inspector (like you named it LogedIn above) and it
should be set in the Main Interface (Target >> General >> Main
Interface).
See rootViewController? The rootViewController
for the window needs to be assigned (either via Storyboard or
programmatically). Please check. I have added a UINavigationController in my example below -
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"StoryboardName" bundle:nil];
ViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"StoryboardName"];
vc.title = #"Is this title visible?";
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = nav;
return YES;
}
For the ViewController.m
-(void) goToSettings {
UIStoryboard *settingsStoryboard = [UIStoryboard storyboardWithName:#"SettingsStoryboard" bundle:nil];
UIViewController *initialSettingsVC = [settingsStoryboard instantiateInitialViewController];
initialSettingsVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:initialSettingsVC animated:YES completion:NULL];
}

Presenting a view for the first time in appDelegate

The goal:
When my app starts up - I need it to display a view before it gets to the "Home" screen. Its a tab bar application and this view is not part of the tabbar.
I am using Storyboards and Xcode 5 - iOS7 only app.
The problem:
I have code that will check if the app is first launch or not. Based on that, I then want to present a one time only view to the user.
What I have tried:
The following code is in the appDelegate of the application as this is where it all starts. I call the following bit of code in there:
-(void)showCountrySettings
{
if (self.termsHaveBeenAccepted){
BBCounterySettingsViewController *countrySettings = [[BBCounterySettingsViewController alloc]initWithNibName:#"View" bundle:nil];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"IDENTIFIER"];
[self.navigationController pushViewController:vc animated:YES];
}
I get compile errors as [self.navigationController..] doesn't exist. Nor does [self.tabbarcontroller...];
This is obvious as I don't have properties setup for these - but how do I go about resolving this and connecting the tab bar to the storyboard?
What am I missing?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
if(!isAgreementAccepted)
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"IDD"];
self.window.rootViewController=vc;
}
return YES;
}
If agreement is not accepted set the T&C viewController as rootViewController when user click the accept button then set the TabBarviewController as root.
u can access the widow object through application delegate any where
[[[UIApplication sharedApplication]delegate] window].rootViewController=tabViewController.
Change the rootviewcontroller of window programaticaly
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *aStoryBoard=[UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
UITabBarController *aTabCtrl=[aStoryBoard instantiateViewControllerWithIdentifier:#"Tab"];
FirstVC *aFirstCtrl=[aStoryBoard instantiateViewControllerWithIdentifier:#"First"];
if(self.termsHaveBeenAccepted)
self.window.rootViewController=aFirstCtrl;
else
self.window.rootViewController=aTabCtrl;
return YES;
}
This will definitely work I have tested.
If Tabbarcontroller is your root viewcontroller, then using below code will resolve the issue in your appdelegate.
-(void)showCountrySettings
{
if (self.termsHaveBeenAccepted){
BBCounterySettingsViewController *countrySettings = [[BBCounterySettingsViewController alloc]initWithNibName:#"View" bundle:nil];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"IDENTIFIER"];
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
[tabBarController presentModalviewcontroller:vc animated:YES completionblock:nil];
}
add your viewcontroller superview of tabbarcontroller as first time else call tabbarcontroller alone

Load a specific controller in didReceiveRemoteNotification

I'm developing push notification on my app. I want that when people tap on push message, app open a specific controller;
If I do that with:
storyBoardName = #"MyStoryboardName";
UIStoryboard* storyBoard = [UIStoryboard storyboardWithName:storyBoardName bundle:nil];
MainWebController* MainWeb = [storyBoard instantiateViewControllerWithIdentifier:#"MainWeb"];
MainWeb.urlToLoad = URL_TO_LOAD;
self.window.rootViewController = MainWeb;
[self.window makeKeyAndVisible];
works, I can see MainWeb when i tap on push massage but the controller view cover all the screen: tab bar and navigation bar are hided!
I know that rootviewcontroller is a uitabbar:
NSLog(#"Controller: %#",self.window.rootViewController.debugDescription);
Solved with:
UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;
tabController.selectedIndex = 0;
UINavigationController *navigationController = (UINavigationController *)tabController.selectedViewController;
[navigationController pushViewController:MainWeb animated:YES];
You are replacing the rootViewController by setting it to be a MainWebController. Is this the initial UIViewController in your Storyboard?
From your issue it would seem that the initial UIViewConroller is a TabViewController and what you actually want is to setup a UIViewController inside this.

Resources