Segment Button is not displaying on the navigation bar in iphone - iphone

Hello
I am having problem in displaying segmented button on the navigation bar
I try this code it's not working
I have the Tab Bar For calling this tabBar I use
This code TabBar is working fine but Button on the navigation bar is not displaying
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITabBarController *tabcontroller = [[UITabBarController alloc] init];
UITabBarController *vc1 = [[TMapViewController alloc] init];
UITabBarController *vc2 = [[TShareController alloc]init];
UITabBarController *vc3 = [[TSpeedometerController alloc]init];
UITabBarController *vc4 = [[TReviewsController alloc]init];
NSArray *viewControllers = [NSArray arrayWithObjects:vc1,vc2,vc3,vc4,nil];
[tabcontroller setViewControllers:viewControllers];
[vc1 release];
[vc2 release];
[vc3 release];
[vc4 release];
[self.navigationController pushViewController: tabcontroller animated: YES];
}
And right this code into the TMapViewController.m for calling
The button onto the navigation bar
-(id)init
{
UITabBarItem *tbi = [self tabBarItem];
[tbi setTitle:#"Map"];
UIImage *i = [UIImage imageNamed:#"mapper_f.png"];
[tbi setImage:i];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:#"Activitieslist.png"],
nil]];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 0, 50, 35);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = segmentBarItem;
[segmentBarItem release];
return self;
}
Same thing i use into the
- (void)viewDidLoad {
[super viewDidLoad];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:#"Activitieslist.png"],
nil]];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 0, 50, 35);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = segmentBarItem;
[segmentBarItem release];
[[self.navigationBar topItem] setTitleView:segmentedControl];
[[[self.navigationBar topItem] titleView] setUserInteractionEnabled:YES];
TLocationManager *manager = [[TLocationManager alloc] init];
self.locationManager = manager;
self.locationManager.delegate = self;
[manager release];
TMapRoutes *routes = [[TMapRoutes alloc] init];
self.mapRoutes = routes;
self.mapRoutes.mapView = self.myMapView;
[routes release];
self.myMapView.delegate = self;
/* Run the simulator */
[locationManager startUpdatingLocation];
}
even it not work it give me same error .
Thanks in advance
Harish

Set it as [self.navigationBar titleView:segmentedControl]
-(void)loadView {
UITabBarItem *tbi = [self tabBarItem];
[tbi setTitle:#"Map"];
UIImage *i = [UIImage imageNamed:#"mapper_f.png"];
[tbi setImage:i];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:#"Activitieslist.png"],
nil]];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 0, 50, 35);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
[[self.navigationBar topItem] setTitleView:segmentedControl]; [[[self.navigationBAr topItem] titleView] setUserInteractionEnabled:YES];
[segmentBarItem release];

Related

how to load picker view values from sqlite database in iPhone

I'm just a beginner in iPhone programming.
I have added 3 uipickers in one view.
First picker for employee.
Second picker is for product.
Third picker for customer.
I have created 3 tables in sqlite for the above.
And I have to load the values to pickers from the tables.
Have I to keep 3 pickers in XiB file?
I want the pickers to be displayed when i click on the table cell.(I have a table view obviously...where I will choose the desired value from the picker)
For time being I have added the array statically. But I wanted to load it from the sqlite database.
How to connect?
How to display the values?
What should I do?
Can you please help me.
I've been meddling with it for 4 days. Can Someone please help....
Cathi
Create 3 UiPickerView using Code after the Database read is done
Set The values according to the database
Set separate Tags (1,2,3) for each UIPicker. Then you can get it again every where.
Set the UiPickerView delegates.
Use [yourPicker selectRow:10 inComponent:0 animated:YES]; to set picker selected value
Sample Code to Add UIPicker
-(void)displayPicker{
#try {
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(pickerCancel:)];
cancelBtn.tag = 1;
[barItems addObject:cancelBtn];
[cancelBtn release];
cancelBtn = nil;
/* Uncomment this line to add label to Timer/Counter.
NSString *popUpTitleText = [self getPopUpTitleText:#"My Text"];
UIBarButtonItem *titleBtn = [[UIBarButtonItem alloc] initWithTitle:popUpTitleText style:UIBarButtonItemStylePlain target:nil action:nil];
[barItems addObject:titleBtn];
[titleBtn release];
titleBtn = nil;
*/
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
flexItem.width = 64;
[barItems addObject:flexItem];
[flexItem release];
flexItem = nil;
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(pickerDone:)];
doneBtn.tag = 1;
[barItems addObject:doneBtn];
[doneBtn release];
doneBtn = nil;
[pickerToolbar setItems:barItems animated:YES];
[barItems release];
barItems = nil;
CGRect pickerFrame = CGRectMake(0, 40, 0, 216);
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.tag = 1;
pickerView.dataSource = self;
pickerView.delegate = self;
CGRect pickerRect = pickerView.bounds;
pickerView.bounds = pickerRect;
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 344)];
popoverView.backgroundColor = [UIColor whiteColor];
pickerView.frame = CGRectMake(0, 44, 200, 216);
[pickerView selectRow:1 inComponent:0 animated:YES]; //set your selected (Database) value here.
[popoverView addSubview:pickerToolbar];
[popoverView addSubview:pickerView];
popoverContent.view = popoverView;
//resize the popover view shown
//in the current view to the view's size
popoverContent.contentSizeForViewInPopover = CGSizeMake(200, 244);
//create a popover controller
popoverController.delegate = nil;
if (popoverController.popoverVisible == YES) {
[popoverController dismissPopoverAnimated:YES];
}
[popoverController release];
popoverController = nil;
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
CGRect popoverRect = [self.view convertRect:yourFrame(CGrect)
fromView:[yourView superview]];
popoverRect.origin.y = popoverRect.origin.y + 15;
popoverController.delegate = self;
popoverRect.size.width = MIN(popoverRect.size.width, 100) ;
popoverRect.origin.x = float yourXvalue;
[popoverController presentPopoverFromRect:popoverRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
//release the popover content
[popoverView release];
[popoverContent release];
[pickerToolbar release];
[pickerView release];
popoverView = nil;
popoverContent = nil;
pickerToolbar = nil;
pickerView = nil;
}
#catch (NSException * ex) {
NSLog(#"Exception in YourClass Method: displayPicker() Name:%# Reason:%#",[ex name],[ex reason]);
}
}

when UISegmentcontrol item changing, why it will hide customBadge in IOS 5?

I have added custom Badge on UISegmentControl. In Xcode 4.0.2 SDK 4.3 its working fine. But in Xcode 4.2 SDK 5.0 custom badge is not shown when another item is selected. I dont know why this happens? I'm using following code for IOS 4.3 Xcode 4.0.2:
- (void)viewDidLoad {
[super viewDidLoad];
super.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"wallpaper.png"]];
NSLog(#"Root View Loaded");
segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
#"Home",#"Surveys",#"Results",#"Create",#"Settings",
nil]];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 0, 310, 40);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.tintColor = [UIColor darkGrayColor];
segmentedControl.momentary = NO;
segmentedControl.highlighted = YES;
segmentedControl.selectedSegmentIndex = 0;
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
NSArray *segmentBarArray = [NSArray arrayWithObjects:
segmentBarItem,nil];
[[self appDelegate] setSegmentedControl:segmentedControl];
[[self appDelegate] setSegmentBarArray:segmentBarArray];
[self setToolbarItems:[[self appDelegate] segmentBarArray] animated:NO];
[[self navigationController] setToolbarHidden:NO animated:YES];
[self.navigationItem setHidesBackButton:YES animated:YES];
self.navigationController.toolbar.tintColor = [UIColor blackColor];
int surveycount = [[self appDelegate] getUnreadSurveyCount];
surveyCountBadge = [CustomBadge customBadgeWithString:[NSString stringWithFormat:#"%d",surveycount]];
[surveyCountBadge setFrame:CGRectMake(105, -10, surveyCountBadge.frame.size.width, surveyCountBadge.frame.size.height)];
[segmentedControl addSubview:surveyCountBadge];
if (surveycount == 0) {
[surveyCountBadge setHidden:YES];
}else{
[surveyCountBadge setHidden:NO];
}
}
-(void)segmentAction:(id) sender{
UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
NSLog(#"selected index is %d",segmentedControl.selectedSegmentIndex);
if (segmentedControl.selectedSegmentIndex == 0) {
[self.navigationController popToRootViewControllerAnimated:YES];
} else if(segmentedControl.selectedSegmentIndex ==1){
surveyForMeViewController = [[SurveyForMeView alloc] initWithNibName:#"SurveyForMeView" bundle:nil];
[self.navigationController pushViewController:surveyForMeViewController animated:YES];
} else if(segmentedControl.selectedSegmentIndex ==2){
resultViewController = [[ResultView alloc] initWithNibName:#"ResultView" bundle:nil];
[self.navigationController pushViewController:resultViewController animated:YES];
} else if (segmentedControl.selectedSegmentIndex ==3) {
newSurveyViewController = [[NewSurveyView alloc] initWithNibName:#"NewSurveyView" bundle:nil];
[self.navigationController pushViewController:newSurveyViewController animated:YES];
} else if (segmentedControl.selectedSegmentIndex ==4) {
settingsViewControlle = [[SettingsView alloc] initWithNibName:#"SettingsView" bundle:nil];
[self.navigationController pushViewController:settingsViewControlle animated:YES];
}
}
Any thing else should I mention in this?
You are adding a subview to a segmentedControl. As you don't know how the system redraws the control when another item is selected, you cannot say if your view will be removed or not. Let the control, button or segmentedControl do its work and if you want to add a badge, add it as a subview of its parent.
[surveyCountBadge setFrame:CGRectMake(105.0f+segmentedControl.frame.origin.x,
-10.0f+sementedControl.frame.origin.y, surveyCountBadge.frame.size.width,
surveyCountBadge.frame.size.height)];
[[segmentedControl superView] addSubview: surveyCountBadge];

How to set action in multiple buttons in navigation bar

this is my code
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *segmentTextContent = [NSArray arrayWithObjects:
NSLocalizedString(#"button1", #""),
NSLocalizedString(#"button2", #""),
NSLocalizedString(#"button3", #""), nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] nitWithItems:segmentTextContent];
segmentedControl.selectedSegmentIndex = 0;
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.frame = CGRectMake(0, 0, 400, 30);
self.navigationItem.titleView = segmentedControl;
[segmentedControl release];
}
three buttons are displayed but i don't know how to set action in button help me thanks in advance
add target at the time of segmentedControl creation like below
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
in that selector
-(IBAction) segmentAction:(id)sender{
UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
NSLog(#"Segment clicked: %d", segmentedControl.selectedSegmentIndex);
switch (segmentedControl.selectedSegmentIndex) {
case 0:
self.segmentLabel.text =#"Segment 1 selected.";
break;
case 1:
self.segmentLabel.text =#"Segment 2 selected.";
break;
default:
break;
}
You have to make 3 buttons and add them to an UIToolbar.
- (void)viewDidLoad
{
[super viewDidLoad];
// create a toolbar to have the buttons at the right side of the navigationBar
UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 150, 44.01)];
toolbar.tintColor = [UIColor clearColor];
[toolbar setTranslucent:YES];
// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
// Create button1
UIBarButtonItem *button1 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:#selector(button1Pressed)];
[buttons addObject:button1];
[button1 release];
// Create button2
UIBarButtonItem *button2 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:#selector(button2Pressed)];
[buttons addObject:button2];
[button2 release];
// Create button3
UIBarButtonItem *button3 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:#selector(button3Pressed)];
[buttons addObject:button3];
[button3 release];
// stick the buttons in the toolbar
[toolbar setItems:buttons animated:NO];
//self.toolbarItems = buttons;
[buttons release];
// and put the toolbar in the nav bar
[[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithCustomView:toolbar] autorelease]];
[toolbar release];
}
...
- (void)button1Pressed
{
//do stuff
}
- (void)button2Pressed
{
//do stuff
}
- (void)button3Pressed
{
//do stuff
}

?How to add custom button to UIPopoverController

How to add button to UIPopoverController ?
I am attaching a picture for able to understand very clear.
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView *popoverView = [[UIView alloc] init];
popoverView.backgroundColor = [UIColor blackColor];
UIToolbar *toolbar=[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,400 ,44)];
toolbar.barStyle =UIBarStyleBlackTranslucent;
NSMutableArray *ButtonArray=[[NSMutableArray alloc ]init];
Save=[[UIBarButtonItem alloc ]initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(save_pressed)];
UIBarButtonItem *space=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *cancel=[[UIBarButtonItem alloc ]initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancel_pressed)];
[ButtonArray addObject:cancel];
[cancel release];
[ButtonArray addObject:space];
[space release];
[ButtonArray addObject:Save];
[Save release];
[toolbar setItems:ButtonArray];
[ButtonArray release];
[popoverView addSubview:toolbar];
[toolbar release];
picker = [[UIPickerView alloc] init];
picker.frame = CGRectMake(0, 44, 400, 340);
picker.delegate = self;
picker.dataSource = self;
picker.showsSelectionIndicator = YES;
[popoverView addSubview:picker];
popoverContent.view = popoverView;
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
[popoverController setPopoverContentSize:CGSizeMake(400, 264) animated:NO];
[popoverController presentPopoverFromRect:btn.frame inView:scr_AddLocation permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
[popoverView release];
[popoverContent release];
here I m adding Picker To Popover When We Pressed Button .
The cancel button (if that is what you are referring to) is the rightBarButtonItem of the UINavigationController's navigation bar. You can set it to be a standard UIBarButtonItem (as is done here) or a custom one (whose view you can create).

UIToolbar and UINavigationController

I need to display a view modally. The viewcontroller that needs to be displayed modally has to have a UIToolbar at the bottom. In this toolbar there are one uisegmentedcontroller with three elements. (Think of an tabbar).
In the viewcontroller that presents the modal viewcontroller I have:
-(IBAction)presentModally:(id)sender {
if (self.nvc == nil) {
MyModalViewController *vc = [[MyModalViewController alloc] init];
UINavigationController *navvc = [[UINavigationController alloc] initWithRootViewController:vc];
navvc.navigationItem.prompt = #"";
navvc.navigationBar.barStyle = UIBarStyleBlack;
[vc release];
self.nvc = navvc;
[navvc release];
}
[self presentModalViewController:self.nvc animated:YES];
}
MyModalViewController:
- (void)loadView {
[super loadView];
UIView *uiview = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 460.0f)];
uiview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.view = uiview;
[uiview release];
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 436.0f, 320.0f, 44.0f)];
toolbar.barStyle = UIBarStyleBlack;
toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
NSArray *itemArray = [NSArray arrayWithObjects: #"One", #"Two", #"Three", nil];
UISegmentedControl *segCon = [[UISegmentedControl alloc] initWithItems:itemArray];
segCon.frame = CGRectMake(60, 4, 200, 36);
segCon.segmentedControlStyle = UISegmentedControlStyleBar;
segCon.tintColor = [UIColor darkGrayColor];
segCon.selectedSegmentIndex = 0;
[segCon addTarget:self action:#selector(changedSegment:) forControlEvents:UIControlEventValueChanged];
[toolbar addSubview:segCon];
self.segmentedControl = segCon;
[segCon release];
[[self navigationController].view addSubview:toolbar];
[toolbar release];
}
- (void)changedSegment:(id)sender {
UISegmentedControl *control = (UISegmentedControl *)sender;
int index = control.selectedSegmentIndex;
[[self navigationController] popViewControllerAnimated:NO];
[[self navigationController] pushViewController:[self.controllers objectAtIndex:index] animated:NO];
}
The viewcontrollers in the array are just normal UIViewControllers.
I have set this property in those classes to:
self.navigationItem.hidesBackButton = YES;
My question: Is this the proper way to achieve a UITabBarController behavior?
Haven't tested it, but It looks good, except that popViewControllerAnimated. I'd use popToRootViewControllerAnimated instead (In case a controller uses itself the navigationController).