So I have to admit, that I had this code working perfectly up until a day or so ago when I added a new view, so I'm rather frustrated at the moment.
The setup:
I have a storyboarded app that contains a tabbar. In the AppDelegate, I have the following for attaching the CoreData
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UINavigationController *navigationController = [[tabBarController viewControllers] objectAtIndex:0];
GamesViewController *controller = [[navigationController viewControllers] objectAtIndex:0];
controller.managedObjectContext = self.managedObjectContext;
return YES;
}
Also in the AppDelegate, I had this method that would set the standard tab background to an image of my choosing:
- (void)customizeInterface {
UIImage* tabBarBackground = [UIImage imageNamed:#"tab_background"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
}
So this all worked fine until I added another login view that precedes my tabs. I had to change what the CoreData was initially being set too (from my tabs to my login/initializing view). Below is the new setup of how the storyboard looks.
Now when the app loads up... the background image appears initially as before, but only on the first tab. Once I click off if it, it switches to the default gradient color again. If I go back to the first / initial tab, the background does not-reapply itself, it stays as the colored gradient.
Here is the amended applicationDidFinishLaunching code to go along with it:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//instantiate local context
NSManagedObjectContext *context = [self managedObjectContext];
if (!context) {
// Handle the error.
NSLog(#"Error: Context is null");
}
LoginViewController *rootViewController = [LoginViewController alloc];
rootViewController.managedObjectContext = context;
return YES;
}
So then what I tried to do, is go into the viewDidLoad in the first VC that my tabbar loads up (GameViewController) and tried adding this to fix the problem:
- (void)viewDidLoad {
[super viewDidLoad];
[self.tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:#"dock_background"]];
}
That didn't work, so I also tried using the same original code that I had in my AppDelegate and that also didn't work:
- (void)viewDidLoad {
[super viewDidLoad];
UIImage* tabBarBackground = [UIImage imageNamed:#"tab_background"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
}
So... I'm sort of stuck. I've got to be doing (or not doing) something so obvious... Anyone out there have any tips / pointers?
Thanks a ton
- Drew
Delegate.h file
#interface AppDelegate : UIResponder
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UIImageView *imgV;
#property (strong, nonatomic) UIViewController *viewController;
#property (strong, nonatomic) UITabBarController *tabBarController;
#end
Delegate.m file
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[DeskboardVctr alloc] initWithNibName:#"DeskboardVctr" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.delegate=self;
self.imgV=[[UIImageView alloc] init];
self.imgV.frame=CGRectMake(0, 0, 1024, 49);
[[self.tabBarController tabBar] insertSubview:self.imgV atIndex:1];
self.tabBarController.delegate=self;
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
Tabbar Delegate Methods
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
NSUInteger index=[[tabBarController viewControllers] indexOfObject:viewController];
switch (index) {
case 0:
self.imgV.image=[UIImage imageNamed:#"t1.png"];
break;
case 1:
self.imgV.image=[UIImage imageNamed:#"t2.png"];
break;
case 2:
self.imgV.image=[UIImage imageNamed:#"t3.png"];
break;
case 3:
self.imgV.image=[UIImage imageNamed:#"t4.png"];
break;
case 4:
self.imgV.image=[UIImage imageNamed:#"t5.png"];
break;
default:
break;
}
return YES;
}
Hope you may help this
thanks
Related
*iam a beginner in iphone development.
i have one problem in uitextview...wat iam trying to do is dragging the the selected string in uitextview from out of the textview...and drag it to the tabbarcontroller is it possible plz any on help me on this....*this is the code ihave return up to now....plz help me
#
import <UIKit/UIKit.h>
#import "TabViewController.h"
#class TabBarViewController;
#interface TabBarAppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate>
{
TabBarViewController *txtviewcontroller;
UITabBarController *tabbar;
NSArray *viewcontrollerarray;
}
#property(nonatomic,retain)NSArray *viewcontrollerarray;
#property(nonatomic,strong)UITabBarController *tabbar;
#property(nonatomic,retain)TabBarViewController *txtviewcontroller;
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) TabBarViewController *viewController;
#end
#import "TabBarAppDelegate.h"
#import "TabViewController.h"
#implementation TabBarAppDelegate
#synthesize txtviewcontroller,tabbar,viewcontrollerarray;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor=[UIColor whiteColor];
self.tabbar=[[UITabBarController alloc]init];
txtviewcontroller=[[TabBarViewController alloc]init];
tabbar.delegate=self;
viewcontrollerarray=[[NSArray alloc]initWithObjects:txtviewcontroller, nil];
self.tabbar.viewControllers=viewcontrollerarray;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[TabBarViewController alloc] initWithNibName:#"TabBarViewController_iPhone" bundle:nil];
} else {
self.viewController = [[TabBarViewController alloc] initWithNibName:#"TabBarViewController_iPad" bundle:nil];
}
self.window.rootViewController = self.tabbar;
[self.window makeKeyAndVisible];
return YES;
}
#import <UIKit/UIKit.h>
#interface TabBarViewController : UIViewController
{
UITextView *textview;
}
#property(nonatomic,retain)UITextView *textview;
#end
#import "TabViewController.h"
#import "TabBarAppDelegate.h"
#include <QuartzCore/CoreAnimation.h>
#interface TabBarViewController ()
#end
#implementation TabBarViewController
#synthesize textview;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.title=#"firstname";
CGRect textViewFrame = CGRectMake(20.0f, 20.0f, 280.0f, 124.0f);
textview = [[UITextView alloc] initWithFrame:textViewFrame];
textview.backgroundColor=[UIColor clearColor];
textview.textColor=[UIColor blackColor];
textview.editable=NO;
NSString *filePath=[[NSBundle mainBundle]pathForResource:#"satyadetails" ofType:#"txt"];
NSString *contentString=[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
textview.text=contentString;
textview.layer.borderWidth = 3.0f;
textview.layer.borderColor = [[UIColor grayColor] CGColor];
textview.returnKeyType = UIReturnKeyDone;
[self.view addSubview:textview];
enter code here
}
Step 1. Get event when user touches inside textview (You can get that by UITextView's Delegate (startEditing delegate))
Step 2. Add a UILabel on ur view giving position where user touches in textview and giving text as that of textview and giving clear color as backgroundcolor. (Do this inside delegate of textview)
Step 3. Inside touches move of ur view change the positions of ur label dynamically according to touches.
Step 4. When user moves its touches till dropping textview delegate will be called check there if(textview==droppingtextview) then put draggingtextview.text=label.text. and remove label from superview.
I have added Two Tabs in my app, to load two view controller using these tabs
Tab1 : Home
Tab2 : Favourite
so I have written below code to achieve this
In app Delegate
AppDelegate.h
#class ViewController,FavViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) ViewController *viewController;
#property (strong, nonatomic) FavViewController *favViewController;
#property (strong, nonatomic) UITabBarController *tabBarController;
#end
AppDelegate.m
#implementation AppDelegate
#synthesize window = _window;
#synthesize viewController;
#synthesize favViewController;
#synthesize tabBarController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
baseTabBarController = [[UITabBarController alloc]init];
baseTabBarController.delegate=self;
viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UINavigationController *homeView = [[UINavigationController alloc]initWithRootViewController:viewController];
favViewController = [[FavViewController alloc] initWithNibName:#"FavViewController" bundle:nil];
favViewController.title = #"My Favourite";
UINavigationController *favouriteView = [[UINavigationController alloc] initWithRootViewController:favViewController];
NSArray *controllers = [NSArray arrayWithObjects:homeView,favouriteView, nil];
baseTabBarController.viewControllers = controllers;
[self.window addSubview:baseTabBarController.view];
[self.window makeKeyAndVisible];
return YES;
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)selectedViewController
{
if (tabBarController.selectedIndex == 0) {
}else if (tabBarController.selectedIndex == 1) {
[(FavViewController *)[(UINavigationController*)selectedViewController topViewController] getData];
}else if (tabBarController.selectedIndex == 2) {
NSLog(#"2");
}
}
and here is the Result Screen I am getting
So Where I am having trouble in this..
If I switch to the next screen my First tab doesn't load First Screen (Home screen) instead just stay on current screen.
Let me try with example
There are four screens in my app let say A, B, C, D
I have added Tabs for A and C screens,those are available in whole app
(All screen).
Now if I'll start app and go A ->B->C -> D and press on Home Tab (A)
it doesn't load Screen A, instead just stay on Current screen
but good thing is if i do same process with another tab C (My
Favourite) it loads correct screen.
Edit: I have implemented didSelectViewController method as #sanjit suggested me but in that I not able distinguish, which tab is tapped this time?
I would be really appreciate!! if someone can point me on right direction
Use Tabbar delegate and using parameter instance of uiviewcontroller call poptorootviewcontroller method. Hope it may work for you. Try the following code
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
UINavigationController *navController = (UINavigationController*)tabBarController.selectedViewController;
[navController popToRootViewControllerAnimated:YES];
}
replace the code
[viewController setTabBarItem:tab1];
[favViewController setTabBarItem:tab2];
with
[[tabbarController tabBar] setItems:[NSArray arrayWithObjects:tab1,tab2, nil]];
that should resolve.
Viewcontroller.h
#property(nonatomic, retain) UITabBarItem *item1;
#property(nonatomic, retain) UITabBarItem *item2;
#property(nonatomic, retain) UITabBarItem *item3;
#property(nonatomic, retain) UITabBarItem *item4;
#property(nonatomic, retain) IBOutlet UITabBar *tabBar;
Viewcontroller.m
#synthesize item1,item2,item3,item4,tabBar;
- (void)viewDidLoad
{
item1 = [[UITabBarItem alloc] initWithTitle:#"Home" image:[UIImage imageNamed:#"home.png"] tag:0];
item2 = [[UITabBarItem alloc] initWithTitle:#"My Lists" image:[UIImage imageNamed:#"mylist"] tag:1];
item3 = [[UITabBarItem alloc] initWithTitle:#"Search" image:[UIImage imageNamed:#"search.png"] tag:2];
item4 = [[UITabBarItem alloc] initWithTitle:#"Settings" image:[UIImage imageNamed:#"setting.png"] tag:3];
NSArray *items = [NSArray arrayWithObjects:item1,item2,item3,item4, nil];
[tabBar setItems:items animated:NO];
[tabBar setSelectedItem:[tabBar.items objectAtIndex:2]];
tabBar.delegate=self;
[self.view addSubview:tabBar
];
}
/****Function Call******/
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
if(item.tag == 0)
{
Newsfeeds *obj = [[Newsfeeds alloc]initWithNibName:#"Newsfeeds" bundle:Nil];
[self.navigationController pushViewController:obj animated:NO];
}
else if(item.tag == 1)
{
MyLists *obj = [[MyLists alloc]initWithNibName:#"MyLists" bundle:Nil];
[self.navigationController pushViewController:obj animated:NO];
}
else if(item.tag == 3)
{
Settings *obj = [[Settings alloc]initWithNibName:#"Settings" bundle:Nil];
[self.navigationController pushViewController:obj animated:NO];
}
}
When I do an analyze in xcode, I get this memory leak on the navController in my appdelegate.m.
The app runs fine, but I just can't get this warning to go away. Can anyone help? Is this ok?
Really appreciate anyone help.
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
// create window and set up view controller
[window addSubview:navController.view];
[window makeKeyAndVisible];
navController.topViewController.title = SHKLocalizedString(#"Quick Lomo Pro");
navController.navigationBar.tintColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1.0];
navController.navigationBar.translucent = NO;
[navController setToolbarHidden:YES];
}
You are never releasing the "UINavigationController" after you call the init.
You should store a pointer to the navigation controller and release it in you appdelegate dealloc method.
#interface AppDelegate : NSObject <UIApplicationDelegate> {
UINavigationController *navController;
}
#end
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
navController = [[UINavigationController alloc] init];
...
}
-(void) dealloc {
[navController release];
[super dealloc];
}
#end
can we use UITab bar controller in view base application thank you
Yes you can.
you can look at the "TheElements" example that apple provides.
you can find it here:
https://developer.apple.com/library/ios/#samplecode/TheElements/Introduction/Intro.html
look at the app delegate.
its very strait forward example.
for you request i tried to make a simple example:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self setupPortraitUserInterface];
return YES;
}
- (UINavigationController *)AchievementsControllerWrappingViewController:(NSInteger*)tabIndex{
switch(tabIndex){
case 0:
FirstViewController *theViewController;
theViewController = [[FirstViewController alloc] init];
break;
case 1:
SecondViewController *theViewController;
theViewController = [[SecondViewController alloc] init];
break;
}
UINavigationController *theNavigationController;
theNavigationController = [[UINavigationController alloc] initWithRootViewController:theViewController];
[theViewController release];
return theNavigationController;
}
- (void)setupPortraitUserInterface {
UINavigationController *localNavigationController;
UIWindow *localWindow;
localWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window = localWindow;
[localWindow release];
tabBarController = [[UITabBarController alloc] init];
NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:2];
for(int i=0;i<2;i++){
localNavigationController = [self AchievementsControllerWrappingViewController:i];
[localViewControllersArray addObject:localNavigationController];
[localNavigationController release];
}
tabBarController.viewControllers = localViewControllersArray;
[localViewControllersArray release];
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
}
i am not next to xCode and i did it with text edit, so please check it when you use it.
shani
Yes we can..
For that you have to create UITabBarViewController and its Object then refrence it with you Application..like :
in your AppDelegate.h
#interface youAppDelegate.h : UIApplicationDelegate {
UIWindow *window;
YourViewController *viewController;
// Declare Your TabBarController Here
UITabBarController *tabBar;
}
#property (nonautomic, retain) IBOutlet UIWindow *window;
#property (nonautomic, retain) IBOutlet TabBarViewController *tabBar;
#end
in you implementation file's ApplicationDidFinish Launching add the following Code
viewController = [[YourViewController alloc] init];
[viewController addSubView:tabBar];
[self.window addSubView:viewController];
In interface builder you have to add the TabBarController to your MainWindow and relate it with the IBOutLet. Give what ever view you want to display in tabBar.
enjoy..
This is a very important auto rotate issue and easy to reproduce.
My application has a UITabBarController. Each tab is a UINavigationController. Auto rotation is handled with normal calls to shouldAutorotateToInterfaceOrientation and didRotateFromInterfaceOrientation.
The interface rotates normally until I call UIViewController.popViewControllerAnimated and change UITabBarController.selectedIndex.
Steps to reproduce:
Create a demo Tab Bar Application.
Add the following code to the App Delegate .h file:#import <UIKit/UIKit.h>
#interface TestRotationAppDelegate : NSObject {
UIWindow *window;
UITabBarController *tabBarController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
#end
// Redefine the interface to cach rotation messages
#interface UITabBarController (TestRotation1AppDelegate)
#end
Add the following code to the App Delegate .m file:#import "TestRotationAppDelegate.h"
#implementation TestRotationAppDelegate
#synthesize window;
#synthesize tabBarController;
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
return YES;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
-(void)dealloc {
[tabBarController release];
[window release];
[super dealloc];
}
#end
#implementation UITabBarController (TestRotation1AppDelegate)
-(void)viewDidLoad {
[super viewDidLoad];
// Add a third tab and push a view
UIViewController *view1 = [[[UIViewController alloc] init] autorelease];
view1.title = #"Third";
UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:view1] autorelease];
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObjectsFromArray:self.viewControllers];
[array addObject:nav];
self.viewControllers = array;
// Push view2 inside the third tab
UIViewController *view2 = [[[UIViewController alloc] init] autorelease];
[nav pushViewController:view2 animated:YES];
// Create a button to pop view2
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(50, 50, 220, 38);
[button setTitle:#"Pop this view" forState:UIControlStateNormal];
[button addTarget:self action:#selector(doAction) forControlEvents:UIControlEventTouchUpInside];
[view2.view addSubview:button];
}
-(void) doAction {
// ROTATION PROBLEM BEGINS HERE
// Remove one line of code and the problem doesn't occur.
[self.selectedViewController popViewControllerAnimated:YES];
self.selectedIndex = 0;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
#end
The interface auto rotates normally until you tap the button on tab #3.
Your help will be geatly appreciated!
iPhone SDK 3.2 solves this issue.
With previous SDK use [self.selectedViewController popViewControllerAnimated:NO].