self.title sets navigationController and tabBarItem's title? Why? - iphone

I do this in a UIViewController for one of my tabs:
self.title = #"Welcome";
However, it's overwriting whatever I have for the tabBarItem. I have tried:
self.tabBarItem.title = #"Home";
and
[self.tabBarItem initWithTitle:#"Home" image:[UIImage imageNamed:#"iconHome.png"] tag:0];
But still, self.title overwrites the tabBarItem, regardless of whether I am trying the two latter pieces of code after the title has been set. The code even runs without errors, but the self.tabBarItem.title or initWithTitle doesn't do anything?

OK, I figured it out! Here's what I am doing:
self.title = #"Title for TabBarItem"; // TabBarItem.title inherits the viewController's self.title
self.navigationItem.title = #"Title for NavigationBar";
the navigationBar would inherit self.title, unless otherwise set using self.navigationItem.title

//set nav item title
self.navigationController.navigationBar.topItem.title = #"zurück";
this did it for me :=)
(nothing of the above worked)

Try:
[self setTitle:#"Welcome"];
UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:#"Home" image:[UIImage imageNamed: image] tag:0];
[self setTabBarItem:item];
[item release];

I was also facing the same issue, but i solve this issue like this. I set the title and image of tabBarItem right after i created them in appDelegate.
This is what i have done:
[viewController setTitle:#"controllerTitle"];
[[viewController tabBarItem] setTitle:#"Custome Title for tab"];
[[viewController tabBarItem] setImage:[UIImage imageNamed:#"tab.png"]];

Related

Populate Table With Help of PopOverController - Objective C

I am using a UIButton, on clicking it I want to display the contents that are present in my NSMutableArray in UITableView with the help of UIPopOverController i.e. I want a UITableView to pop up whose cells show the contents of my NSMutableArray.
I am using the following lines of code:
UITableViewController *table= [[UITableViewController alloc]init];
table.delegate = self;
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:table];
self.popoverController = popover;
popoverController.delegate = self;
NSString *hDir = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString *hFilePath = [hisDir stringByAppendingPathComponent:#"hhhh.txt"];
NSArray *array = [NSArray arrayWithContentsOfFile:hFilePath ];
NSMutableArray *kkkk = [[NSMutableArray alloc] init];
for (NSDictionary *dict in array) {
[kkkk addObjectsFromArray:[dict allKeys]];
}
table = [[UIImageView alloc] initWithFrame:[window bounds]];
// Set up the image view and add it to the view but make it hidden
[window addSubview:table];
table.hidden = YES;
[window makeKeyAndVisible];
I am unable to get the UITableView to pop up on the press of my UIButton. Can anyone help me to sort it out?
One way to show the UITableView in the UIPopOverController is by creating a new UIViewController class. And invoking it in initWithContentViewController method of UIPopOverController.
1. Create a new UIViewController or UITableViewController class. Create an instance of it.
2. Use the instance in the initWithContentViewController method of UIPopOverController.
3. Mention from where it should "pop"
For Example in your Button action :
-(IBAction)yourButtonAction:(id)sender
{
YourNewViewController*newVC=[[YourNewViewController alloc]init];
UIPopoverController*somePopOver=[[UIPopoverController alloc]initWithContentViewController:catergoryVC]; //Tell which view controller should be shown
[somePopOver setPopoverContentSize:CGSizeMake(200, 200)]; // set content size of popover
[somePopOver presentPopoverFromRect:yourButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES]; //From where it should "pop"
}
It seems you want to present it from a UIBarButtonItem so instead of presentPopoverFromRect use presentPopoverFromBarButtonItem
[somePopOver presentPopoverFromBarButtonItem:yourBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
If you want to display popover on press of button click, then first add your button in viewcontroller, display that view controller as follows:
In app delegate write code:
MyViewController *viewController = [[MyViewController alloc] init];
[self.window addSubView:viewController.view];
In MyViewController add button and provide target to that button displayPopup as follows:
-(void)displayPopup:(id)sender
{
UITableViewController *tblViewPopover = [[UITableViewController alloc] init];
tblViewPopover.tableView.delegate = self;
tblViewPopover.tableView.dataSource = self;
tblViewPopover.tableView.backgroundColor = [UIColor whiteColor];
tblViewPopover.tableView.separatorStyle= UITableViewCellSeparatorStyleSingleLine;
float theWidth = 280;
tblViewPopover.contentSizeForViewInPopover = CGSizeMake(theWidth,200);
if(m_popOvrController){
[m_popOvrController dismissPopoverAnimated:NO];
[m_popOvrController release];
m_popOvrController=nil;
}
m_popOvrController = [[UIPopoverController alloc] initWithContentViewController:tblViewPopover];
[tblViewPopover release];
[m_popOvrController presentPopoverFromRect:sender.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO];
}
and you can use tableview delegate methods to display data in tableview.
I think, UIPopoverViewController is initialized using UIViewController and here you are using UIView(UITableView).
Can you please try using UITableViewController instead?
Also, if things does not work according to plan when you create it using the code try using an XIB explicitely.
This should help.

UINavigationController within a UITabBarController, setting the navigation controller title

In my app I have a UINavigationController within a UITabBarController. Everything is working fine, however I can't set the title for the navigation controller. I have tried several different methods and googled around but there seems to be no solution for this problem.
Any Help would be greatly appreciated.
Thankyou in advance.
Sam
I found the answer in the end, it was:
self.tabBarController.navigationItem.title = #"title";
self.navigationItem.title = #"Your Title"
Works for every case.
For me the following works fine:
Initiate the controllers in appDelegateDidFinishLaunching:Method:
UINavigationController *navContr1;
UINavigationController *navContr2;
UIViewController *viewController1, *viewController2;
viewController1 = [[[FirstViewController alloc] initWithNibName:#"FirstViewController_iPhone" bundle:nil] autorelease];
viewController2 = [[[SecondViewController alloc] initWithNibName:#"SecondViewController_iPhone" bundle:nil] autorelease];
navContr1 = [[[UINavigationController alloc] initWithRootViewController:viewController1] autorelease];
navContr2 = [[[UINavigationController alloc] initWithRootViewController:viewController2] autorelease];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
//self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navContr1, navContr2, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
with this done, in your different viewControllers initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil - Method you can change the title with the following line:
self.title = #"Your Title";
Good Luck.
Look at the UIViewController class in the docs. There is a property pointing to the UINavigationItem which then has a property pointing to the title. If the pointer to the navigation item returns nil, you must have not wired the controllers together correctly.
The self.title always sets the tile of the rootViewController object of your ViewController.
If you have your viewController & the rootViewController is UINavigationController, then self.title will set the title of UINavigationController.
Similarly, if you have initialized a UITabViewController with the UIViewController objects, then the self.title will applies on the title corresponding button at index, on UITabBar of UITabViewController.
Hence said that, self.title applies on the object holding the viewController.

Three20 & TabBarController

I'm stuck on something.
I'm using three20 and the TTTNavigator example. Where I can find a TabBarController that is using :
[self setTabURLs:[NSArray arrayWithObjects:#"tt://crush",
#"tt://crushes",
nil]];
to create each tab.
Ok until here all is ok, my problem is that I have all my tabs without title and image. I know how to set individual when it loads the view controller inside - (void)viewDidLoad :
self.title = #"crush";
UIImage* image = [UIImage imageNamed:self.title];
self.tabBarItem = [[[UITabBarItem alloc] initWithTitle:self.title image:image tag:0] autorelease];
but this is a problem because when you init the app all the tabs except the selected are empty.
Any suggest on how to implement that.
Ok I've resolved, I used the -init method of each view controller.. like usually... but what I need to change was :
[self setTabURLs:[NSArray arrayWithObjects:#"tt://crush/1",
#"tt://crushes/2",
nil]];
And in the map :
[map from:#"tt://crush/(init)" toViewController:[MakeCrushViewController class]];
[map from:#"tt://crushes/(init)" toViewController:[MyCrushesViewController class]];
for me right now it looks pretty weird.. but ok is working, don't ask me why you need to set 1 and 2 before the links.. if I set 1 on each it crashes...
This guy had an answer to this question in this blog post.1
Basically, within your tab view controller, just access the your view controllers
like you would without three20. In your tab view controller implimentation file
-(void)viewDidLoad{
[super viewDidLoad];
NSArray *tabs = [self viewControllers];
UIViewController *home = [tabs objectAtIndex:0];
home.tabBarItem.image = [UIImage imageNamed:#"redo.png"];
UIViewController *activities = [tabs objectAtIndex:1];
activities.tabBarItem.image = [UIImage imageNamed:#"restart.png"];
UIViewController *map = [tabs objectAtIndex:2];
map.tabBarItem.image = [UIImage imageNamed:#"lock.png"];
}

UITabBar does not display

Hi i am working on an application which has a login form and another view called as Invitation. Now heres my problem in my Login view on the hit of Login button i am coming to the Invitation View with the help of the code which looks like this
-(void)buttonPress
{
Invitation *obj = [[Invitation alloc]init];
[self.navigationController pushViewController:obj animated:YES];
}
Now in the Invitation view what happens is that i want a tabbar to be displayed so i wrote a code which looks like this
objTabbar = [[UITabBar alloc]init];
HomeItem = [[UITabBarItem alloc]initWithTitle:#"Home" image:[UIImage imageNamed:#"house.png"] tag:0];
BluetoothItem = [[UITabBarItem alloc]initWithTitle:#"Bluetooth" image:[UIImage imageNamed:#"bluetooth.png"] tag:1];
NSArray *a = [[NSArray alloc]initWithObjects:HomeItem,BluetoothItem,nil];
objTabbar.items = a;
and in the LoadView method i did this
- (void)loadView {
[super loadView];
self.navigationItem.hidesBackButton = YES;
[self.view addSubview:objTabbar];
}
Now the code is building successfully but the problem is that the Tabbar is not getting displayed. I tried tabbarController and did this
tabbarController.view = objTabbar;
and added the tabbarController in the loadView method
[self.view addSubview:tabbarController.view];
But still no success
Please help me out and let me know where am i going wrong
Thank You
i believe the reason why you are not getting is because you are using the wrong code. Here are few mistakes i would like to point out.
replace objTabbar = [[UITabBar alloc]init]; with
objTabbar = [[UITabBarController alloc]init];
Replace objTabbar.items = a; with
[objTabbar setViewControllers:a];

Initial selected Item in an UITabBar without a UITabBarController

How do I set the default selected UITabBarItem in an UITabBar that is within an UIView, not an UITabBarController?
Just to clarify, the UIView does implement the protocol and the didSelectItem method works. At run-time, the tabbar works and the tabbaritems selected when the user touches them. My problem is setting the default selected item.
If i use [myTabbar setSelectedItem] within the didSelectItem method it works. But outside of it, it doesn't (for example, in the viewDidLoad method of my UIView).
Thanks!
Thank you for your approatch, but I still have a problem that the didSelectedItem is not called when I select the item with setSelectedItem. Any Idea?
actually, I did use it a little bit different:
[tabbar setSelectedItem:[tabbar.items objectAtIndex:0]];
finally I solved it this way....
- (void)viewDidLoad
{
UITabBarItem *item = [myTabBar.items objectAtIndex:0];
[self.myTabBar setSelectedItem:item];
if(tab1Exam == nil){
self.tab1Exam = [[CurExam alloc] initWithNibName:#"CurExam" bundle:nil];
[self.view insertSubview:tab1Exam.view belowSubview:myTabBar];
if (currentViewController != nil)
[currentViewController.view removeFromSuperview];
currentViewController = tab1Exam;
}
}
You might want to post some sample code. I just ran a quick test in -viewDidLoad:
UITabBarItem *about = [[UITabBarItem alloc] initWithTitle:#"About" image:[UIImage imageNamed:#"About.png"] tag:0];
NSArray *tabBarItems = [[NSArray alloc] initWithObjects:about,nil];
[tabBar setItems:tabBarItems animated:NO];
[tabBar setSelectedItem:about];
[tabBarItems release];
[about release];
which worked fine, or at least worked as I expected.