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];
Related
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]);
}
}
I want to change default effect of TabBarItem, getting after selecting and deselecting item. I solved this problem for ios5 and later version by using this method
[mySchedule setFinishedSelectedImage:[UIImage imageNamed:#"myschedule.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"aboutus.png"]];
but for version 4 and below it gives problem. Please tell me how i solved this problem for version 4 and below?
Here i've created a custom tab bar which has four buttons. Buttons consists of images and which i took in a constant file. Here the serivceImg, contactImg etc are UIImageView which is declared in .h file. Also, don't forget to add UITabBarControllerDelegate as a delegate in your .h file.
-(void)setUpTabBar {
tabBar = [[UITabBarController alloc] init];
Services *firstViewController = [[Services alloc]init];
firstViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1];
UINavigationController *firstNavController = [[UINavigationController alloc]initWithRootViewController:firstViewController];
ContactUs *secondViewController = [[ContactUs alloc]init];
secondViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:2];
UINavigationController *secondNavController = [[UINavigationController alloc]initWithRootViewController:secondViewController];
Bookings *thirdViewController = [[Bookings alloc]init];
thirdViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:3];
UINavigationController *thirdNavController = [[UINavigationController alloc]initWithRootViewController:thirdViewController];
Reward *fourthViewController = [[Reward alloc]init];
fourthViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemRecents tag:4];
UINavigationController *fourthNavController = [[UINavigationController alloc]initWithRootViewController:fourthViewController];
tabBar.viewControllers = [[NSArray alloc] initWithObjects:firstNavController, secondNavController, thirdNavController, fourthNavController, nil];
tabBar.delegate=self;
tabBar.selectedIndex=0;
[firstNavController release];
[firstViewController release];
[secondNavController release];
[secondViewController release];
[thirdNavController release];
[thirdViewController release];
[fourthNavController release];
[fourthViewController release];
serivceImg=[[UIImageView alloc]initWithFrame:CGRectMake(0, 432, 80, 49)];
serivceImg.image=[UIImage imageNamed:serviceHover];
contactImg=[[UIImageView alloc]initWithFrame:CGRectMake(81, 432,80, 49)];
contactImg.image=[UIImage imageNamed:tabContact];
bookingImg=[[UIImageView alloc]initWithFrame:CGRectMake(162, 432,80, 49)];
bookingImg.image=[UIImage imageNamed:tabBooking];
rewardImg=[[UIImageView alloc]initWithFrame:CGRectMake(243, 432, 80, 49)];
rewardImg.image=[UIImage imageNamed:tabReward];
[tabBar.view addSubview:serivceImg];
[tabBar.view addSubview:contactImg];
[tabBar.view addSubview:bookingImg];
[tabBar.view addSubview:rewardImg];
[[[UIApplication sharedApplication]keyWindow]addSubview:tabBar.view];
[serivceImg release];
[contactImg release];
[bookingImg release];
[rewardImg release];
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController1{
if (viewController1 == [tabBar.viewControllers objectAtIndex:0])
{
serivceImg.image = [UIImage imageNamed:kserviceHover];
contactImg.image=[UIImage imageNamed:ktabContact];
bookingImg.image=[UIImage imageNamed:ktabBooking];
rewardImg.image=[UIImage imageNamed:ktabReward];
}
else if (viewController1 == [tabBar.viewControllers objectAtIndex:1])
{
serivceImg.image = [UIImage imageNamed:ktabService];
contactImg.image=[UIImage imageNamed:kcontactHover];
bookingImg.image=[UIImage imageNamed:ktabBooking];
rewardImg.image=[UIImage imageNamed:ktabReward];
}
else if (viewController1 == [tabBar.viewControllers objectAtIndex:2])
{
serivceImg.image = [UIImage imageNamed:ktabService];
contactImg.image=[UIImage imageNamed:ktabContact];
bookingImg.image=[UIImage imageNamed:kbookingHover];
rewardImg.image=[UIImage imageNamed:ktabReward];
}
else if (viewController1 == [tabBar.viewControllers objectAtIndex:3])
{
serivceImg.image = [UIImage imageNamed:ktabService];
contactImg.image=[UIImage imageNamed:ktabContact];
bookingImg.image=[UIImage imageNamed:ktabBooking];
rewardImg.image=[UIImage imageNamed:krewardHover];
}
}
Hope this solves your problem.
Remove .png from the image name.
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];
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).
I Created Segment Control through Interface Builder.
Created a IBAction and Linked to Value Changed Option of segment Controller.
- (IBAction)GenderBttonAction:(id)sender {
printf("\n Segemt Controll");
}
When i click on segment controller this method is calling , but how would i get the seleced index value of segment controller.
Please help me dears.
((UISegmentedControl *)sender).selectedSegmentIndex;
:-)
I use the following code in a view controller.m to say launch a modal controller and it seems to work good for me.
- (void)viewDidLoad
{
NSArray *segmentContent = [NSArray arrayWithObjects:
NSLocalizedString(#"view 1", #""),
NSLocalizedString(#"view 2", #""),
NSLocalizedString(#"Close", #""),
nil];
//or images insted of text
//NSArray *segmentContent = [NSArray arrayWithObjects:
// [UIImage imageNamed:#"pic.png"],
// [UIImage imageNamed:#"spic.png"],
// [UIImage imageNamed:#"close.png"],
// nil]];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentContent];
segmentedControl.selectedSegmentIndex = UISegmentedControlNoSegment;
segmentedControl.momentary = YES; // option
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.frame = CGRectMake(0, 0, 160, 30);
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *segments = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = segments;
self.navigationItem.title = #"My title";
[segments release];
Then add the actions on selection, last one is a close statement if you launched a modal controller.
- (IBAction)segmentAction:(id)sender
{
UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
switch ([sender selectedSegmentIndex])
{
case 0:
{
// Do stuff like launch a modal controller. But don't forget to add this all into your modal controller views to get back out :)
InfoViewController *infoViewController = [[InfoViewController alloc] initWithNibName:#"InfoViewController" bundle:nil];
UINavigationController *aInfoViewController = [[UINavigationController alloc] initWithRootViewController:infoViewController];
[self presentModalViewController:aInfoViewController animated:YES];
break;
}
case 1:
{
// do stuff
break;
}
case 2:
{
[self.parentViewController dismissModalViewControllerAnimated:YES];
break;
}
}
NSLog(#"Segment clicked: %d", segmentedControl.selectedSegmentIndex);
}
Use the method below if needed to close the modal via this way.
- (IBAction)dismissAction:(id)sender
{
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
and don't forget to declare the action/method in the same respective h file.
- (IBAction)dismissAction:(id)sender;
Hope this helps.
Sincerely, Kirk