Initial selected Item in an UITabBar without a UITabBarController - iphone

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.

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.

Data for UITableView gets changed

I'm trying to create a Settings icon for the rightBarButtonItem for my UINavigationController. In my application:DidFinishLaunching, I create the button, and set it
//pseudo code for applicationdidfinish launching
HomeController *home = [[HomeController alloc] init]; // root view of my UINavigationController
home.navigationItem.rightBarButtonItem = settingsBarButtonItem;
[settingsButton addTarget:self action:#selector(settingsPressed:) forControlEvents:UIcontrolEventTouchUpInside]; // i used a button for the barbuttonitem to not get the bar button item border
than in settingsPressed:
SettingsController* settings = [[SettingsController alloc] initWithNibName:#"SettingsController" bundle:nil];
UINavigationController* popoverNav = [[UINavigationController alloc] initWithRootViewController:settings];
[settings release];
popover = [[UIPopoverController alloc] initWithContentViewController:popoverNav];
[popoverNav release];
// show settings
if ([popover isPopoverVisible]) {
[popover dismissPopoverAnimated:YES];
} else {
[popover presentPopoverFromBarButtonItem:bbiSettings permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
in SettingsController, in viewDidLoad:
NSArray *array = [NSArray arrayWithObjects:#"one", #"two", #"three", nil];
self.DataArray = array; // (nonatomic, retain)
[array release];
I do not show this array right away. Like the iPhone Settings app, when they click on one of the cells in my Grouped Table, it opens up a new UITableView. So in that UITableView, in the tableView:numberOfRowsInSection: method, I
return [self.DataArray count];
However, it is here that my app crashes. When I look at my array, I now have random stuff in there, like vl.proj sometimes, UIViews, etc. I do not know why this array gets changed. I do not know if it's because I'm calling the popover from the applicaionDelegate which I normally do not do and that is the problem, or if there is something else wrong. Thanks.
[NSArray arrayWithObjects:] returns an autoreleased array. So you don't have to release it manually. You can just remove the line [array release]; in viewDidLoad and everything should work fine.

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];

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

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"]];