How To Get Values From UISegmentcontroller - iphone

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

Related

UISegmented view to change views

I have set up two barbutton items one on the right and one on the left. The right one successfully changes between two views, and the second one ont the right is a segmented control which is supposed to change between two views like wise. Here is my code, i want to know how to implement changing views with the segmented control.
- (void)setupNavigationBar {
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44.0f)];
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:#"ContainerView"];
navItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward target:self action:#selector(switchViewControllers)];
navBar.items = [NSArray arrayWithObject:navItem];
segmented = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:#"seg1.png"],
[UIImage imageNamed:#"seg1.png"],
nil]];
segmented.frame = CGRectMake(0, 0, 50, 30);
segmented.segmentedControlStyle = UISegmentedControlStyleBar;
UIBarButtonItem * segmentedBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmented];
navItem.leftBarButtonItem = segmentedBarItem;
[self.view addSubview:navBar];
}
- (void)viewDidLoad {
[super viewDidLoad];
// This is just for the purpose of calling the switch method.
[self setupNavigationBar];
CGRect rect = CGRectMake(0, 44, 320, 436);
currentViewController = [[FirstViewController alloc] init];
currentViewController.view.frame = rect;
nextViewController = [[SecondViewController alloc] init];
nextViewController.view.frame = rect;
[self addChildViewController:currentViewController];
[self addChildViewController:nextViewController];
[self.view addSubview:currentViewController.view];
}
- (void)switchViewControllers {
[self transitionFromViewController:currentViewController toViewController:nextViewController duration:3.0 options:UIViewAnimationTransitionFlipFromRight | UIViewAnimationOptionCurveEaseInOut animations:nil completion:^(BOOL finished) {
UIViewController *tempViewController = currentViewController;
currentViewController = nextViewController;
nextViewController = tempViewController;
tempViewController = nil;
}];
}
How would I implement the segmented control to change views?
For add UISegmentedControl on UINavigationBar
EDIT :
NSArray *itemArray = [NSArray arrayWithObjects:
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
nil];
UISegmentedControl* menuSegmentedButton = [[UISegmentedControl alloc] initWithItems:itemArray];
menuSegmentedButton.momentary = YES;
menuSegmentedButton.selected = NO;
menuSegmentedButton.segmentedControlStyle = UISegmentedControlStyleBar;
[menuSegmentedButton addTarget:self action:#selector(doMenu)
forControlEvents:UIControlEventValueChanged];
[menuSegmentedButton sizeToFit];
UIBarButtonItem* barButton = [[UIBarButtonItem alloc]
initWithCustomView:menuSegmentedButton];
self.navigationItem.leftBarButtonItem = barButton;
Add this method for segment button tapped
-(void)doMenu
{
/// code for segment button tapped
}
I guess performance issues may require having separate view controllers rather than two subviews but it is much more complex - You would have a very complicated navigation system and would need to set the segment control for each view. I am sure there is another way to subclass a view but I am unfamiliar with this so would suggest having one view controller containing your views.
Note: If you were to programmatically create all the objects in each view, you could achieve great performance by removing objects from any hidden views.
So first you have a UISegmentedControl (as created in iPatel's answer). But add in this line [ctrl addTarget:self action:#selector(switchViewControllers) forControlEvents:UIControlEventValueChanged];
Then in switchViewControllers put:
if (ctrl.selectedSegmentIndex == 0) {
NSLog(#"One");
self.viewOne.hidden = NO;
self.viewTwo.hidden = YES;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:#"showView" forKey:#"viewOne"];
[defaults synchronize];
}
else if (ctrl.selectedSegmentIndex == 1) {
NSLog(#"Two");
}
-(void)viewDidLoad {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([[defaults objectForKey:#"showView"] isEqualToString:#"viewOne"]) {
self.viewOne.hidden = NO;
self.viewTwo.hidden = YES;
}
else if (...) {
}
}
The above should work when you toggle the segment control. In each if you can toggle the visibility of two views. You would create the different views as a subview of the main view; one on top of the other. You would need to hide one view either in Interface Builder or in the viewDidLoad (I would suggest storing the current view in NSUserDefaults and showing that view when the user first loads the view controller).

UIBarButtonItems of a UIToolbar hiding when presenting UIActivityViewController

This looks like a bug to me, but maybe someone can think of a workaround?
Basically if you have a custom UIToolbar, its button items will automatically hide when you present a UIActivityViewController, and reappear when you dismiss it. This is only the case on the iPhone. Being that UIActivityViewController doesn't hide the entire screen it just looks weird that buttons disappear behind the dimmed screen.
To replicate, simply start a single view project and use the following code on the view controller:
- (void)viewDidLoad {
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40)];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(didTapAction)];
toolbar.items = [NSArray arrayWithObject:button];
[self.view addSubview:toolbar];
}
- (void)didTapAction {
NSArray *items = [NSArray arrayWithObjects:#"Text", nil];
UIActivityViewController *sharing = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
[self presentViewController:sharing animated:YES completion:nil];
}
Found a workaround. Simply get rid of all the items before presenting, and add them back right after.
- (void)didTapAction {
NSArray *items = [NSArray arrayWithObjects:#"Text", nil];
UIActivityViewController *sharing = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
NSArray *barItems = toolbar.items;
toolbar.items = nil;
[self.navigationController presentViewController:sharing animated:YES completion:nil];
toolbar.items = barItems;
}
know it is quite old thread for but those who look this page for solution, here you go :
With iOS7, you can use this approach to show/hide your toolbar button :
if(// your code Condition)
{ self.toolbarBtn1.enabled = YES;
self.toolbarBtn1.tintColor = nil; }
else
{ self.toolbarBtn1.enabled = NO;
self.toolbarBtn1.tintColor = [UIColor clearColor]; }

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
}

Adding buttons to navigation bar

Is it possible to add buttons to the navigation bar using the IPhone SDK?
I already have 2 buttons in the navigation bar as leftBarButton and rightBarButton. I need 2 more buttons. How to implement that?
Its not obligatory that i need them to be included in the navigation bar itself. But since the application contains only a table, i don't think it can be given elsewhere.
You can use the UISegmentedControl. Check the UICatalog code sample to check its usage in the navigation bar.
Here is some sample code:
- (void)viewDidLoad {
[super viewDidLoad];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:#"up.png"],
[UIImage imageNamed:#"down.png"],
nil]];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 0, 90, 35);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = segmentBarItem;
[segmentBarItem release];
}
- (void)segmentAction:(id)sender{
if([sender selectedSegmentIndex] == 0){
//do something with segment 1
NSLog(#"Segment 1 preesed");
}else{
//do something with segment 2
NSLog(#"Segment 2 preesed");
}
}