How to handle events of multiple segmented control in a single view - iphone

I have 2 segmented controls in my viewcontroller view. How can I handle the tap events of both of the segmented controllers?

There are two ways to do so.
Add different actions for every segment control
Add same actions for every segment control & check which control is tapped using its tag.
[yourSegmentedControl addTarget:self action:#selector(segmentSwitch:) forControlEvents:UIControlEventValueChanged];
- (IBAction)segmentSwitch:(id)sender
{
UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
if(segmentedControl.tag == someTag)
{
if(segmentedControl.selectedSegmentIndex == 1)
{
// your code
}
else if(segmentedControl.selectedSegmentIndex == 2)
{
// your code
}
}
else if(segmentedControl.tag == someTag)
{
if(segmentedControl.selectedSegmentIndex == 1)
{
// your code
}
else if(segmentedControl.selectedSegmentIndex == 2)
{
// your code
}
}
}

Apple docs says:
http://developer.apple.com/library/IOs/#documentation/UIKit/Reference/UISegmentedControl_Class/Reference/UISegmentedControl.html
You register the target-action methods for a segmented control using the UIControlEventValueChanged constant as shown below.
[segmentedControl addTarget:self
action:#selector(action:)
forControlEvents:UIControlEventValueChanged];
So, you just have to register action for every segmented control.

Set the tag property on each segmented control to a different integer. Then in your method you set as the action for when the value changes, check which integer the tag property is set to using [sender tag].

You can use selected mode of segment:
UISegmentedControl *tempSegment = sender;
if ([tempSegment selectedSegmentIndex] == 0){
//first Action
}
else if ([tempSegment selectedSegmentIndex] == 1){
//second Action
}

Assign two different actions to these segmented controls:
[segmentedControl addTarget:self
action:#selector(action:)
forControlEvents:UIControlEventValueChanged];

Swift version:
#IBAction func yourFunctionName(sender: UISegmentedControl) {
if (sender.selectedSegmentIndex == 0){//choice 1
}else{//choice 2
}
}

Related

Different callOut in annotationView

I am working on Map iOS6, and I got some troubles:
As the images below, the annotation of current location and placeMark also have the callOut button but I need the button do different task, how can I do that? It means that in current location, I need callOut button for this task and in placeMark, the callout button do another task.
Please visit this page to see the image
http://upanh.com/listing/?s=c3f4954854a30fe50e3e15f9ae064ba2
I have not enough reputation to post the image here.
I tried this code:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control
{
if ([(UIButton*)control buttonType] == UIButtonTypeDetailDisclosure)
{
...
locationSheet.cancelButtonIndex = locationSheet.numberOfButtons - 1;
}else if ([(UIButton*)control buttonType] == UIButtonTypeInfoLight)
{
...
locationSheet.cancelButtonIndex = locationSheet.numberOfButtons - 1;
}
}
and
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
// 1
if (buttonIndex != actionSheet.cancelButtonIndex)
{
if (buttonIndex == 0)
{
}
}
}
How can I do the different task between two button in actionSheet? It's quite hard to explain my situation, I hope everyone understand what I perform above and I appreciate your helps.
Thanks for advance.
I can't get to your images - but if you have actual buttons (UIButton) and not MKAnnotations, then why not specify a tag for your buttons (different tag for each, of course), point them at the same function, and then differentiate based on tags? So (this can be used for any button not just UIButtonTypeCustom, and for any UIView actually - they all support tag):
UIButton *firstButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIButton *secondButton = [UIButton buttonWithType:UIButtonTypeCustom];
firstButton.tag = 100;
secondButton.tag = 200;
[firstButton addTarget:self selector:#selector(doSomething:)];
[secondButton addTarget:self selector:#selector(doSomething:)];
- (void)doSomething:(id)sender {
UIButton *pressedButton = (UIButton*)sender;
if (pressedButton.tag == 100) {
//First button
}
else if (pressedButton.tag == 200) {
//Second button
}

Distinguish if UIsegmentedontrol value change is from user or from system

Hello I have a UISegmentedControl with two segments. The selected segment is modified programmatically in some case and by the user in some other. I only want to trigger the selector when the change is due to a user action(only when user actually press the segmented control and not when the system do segmentedControl.selectedSegmentIndex = ...). Any idea?
If you do
[self.segment setSelectedSegmentIndex:1];
This will not call the action of valueChanged on the segment, so what is your question?
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents: UIControlEventValueChanged];
- (IBAction)segmentAction:(id)sender {
// valuechanged connected function
UISegmentedControl *segControll = (UISegmentedControl *)sender;
if (segControll.tag == 0) {
}
else {
isProgramaticallyChanged = NO; //important
}
}

Unable to keep UIButton in selected state after TouchUpInside Event

I have the need for an UIButton to maintain a pressed state. Basically, if a button is in a normal state, I want to touch the button, it highlight to its standard blue color and then stay blue after lifting my finger.
I made the following UIAction and connected the buttons Touch Up Inside event to it.
-(IBAction) emergencyButtonPress:(id) sender
{
if(emergencyButton.highlighted)
{
emergencyButton.selected = NO;
emergencyButton.highlighted = NO;
}
else
{
emergencyButton.selected = YES;
emergencyButton.highlighted = YES;
}
}
But what happens, after I remove my finger, the button goes back to a white background. For a test I added a UISwitch and have it execute the same code:
-(IBAction) emergencySwitchClick:(id) sender
{
if(emergencyButton.highlighted)
{
emergencyButton.selected = NO;
emergencyButton.highlighted = NO;
}
else
{
emergencyButton.selected = YES;
emergencyButton.highlighted = YES;
}
}
In this case the button toggles to a highlighted and non-highlighted state as I would expect.
Is there another event happening after the Touch Up Inside event that is resetting the state back to 'normal'? How do I maintain the highlighted state?
The highlighted state is applied and removed by iOS when you touch / release the button. So don't depend on it in your action method.
As one of the other answers says, set the highlighted image to be the same as the selected image, and then modify your action method:
-(IBAction) emergencyButtonPress:(id) sender
{
emergencyButton.selected = !emergencyButton.selected;
}
If you want something like a toggle button, customize your "selected" state to be your "pressed" state, and then write something like this:
- (IBAction)buttonTapped:(id)sender {
[(UIButton*)sender setSelected:![sender isSelected]];
}
if you want the above code to work, you should set an image for the state selected and one (even the same) for he state highlighted.
[emergencyButton setBackgroundImage:buttonHighlightImage forState:UIControlStateHighlighted];
[emergencyButton setBackgroundImage:buttonHighlightImage forState:UIControlStateSelected];
hope it helps.
I had this same problem, and this is what worked for me:
-(IBAction)buttonPressed:(id)sender {
if (isSelected) {
[_button setSelected:NO];
[_button setImage:[UIImage imageNamed:#"not_selected.png"] forState:UIControlStateSelected];
isSelected=NO;
}
else {
[_button setSelected:YES];
[_button setImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateSelected];
isSelected=YES;
}
}
This is not possible unfortunately, as soon as you let go Apple changes the state again.
One option I've used before (but not pretty) is that you use a performSelector with delay 0.1 after the button is pressed to put it again in the selected state. This did work in my case.
EDIT: If you don't want to see the blinking effect, just set the delay to 0.0
This worked for me:
- (void)tapButton:(id)sender{
UIButton *button = sender;
if (!button.selected){
[self performSelector:#selector(highlight:) withObject:button afterDelay:0.0];
}else{
[self performSelector:#selector(removeHighlight:) withObject:button afterDelay:0.0];
}
}
- (void)highlight:(UIButton *)button{
button.selected = !button.selected;
button.highlighted = YES;
}
- (void)removeHighlight:(UIButton *)button{
button.selected = !button.selected;
button.highlighted = NO;
}
Try this simple answer....
- (void)mybutton:(id)sender
{
UIButton *button = (UIButton *)sender;
button.selected = ![button isSelected]; // Important line
if (button.selected)
{
NSLog(#"Selected");
NSLog(#"%i",button.tag);
}
else
{
NSLog(#"Un Selected");
NSLog(#"%i",button.tag);
}
}

Can't read the segmentedcontroll index

I have implemented several of UISegmentedControl objects before but for one or other reason I am unable to implement it within a tableview cell. I want the user to choose his gender. if the value of the segmentedControl changed a method gets called to save it for further processing.
self.seg = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:#"Man",#"Female", nil]];
[self.seg addTarget:self action:#selector(toggleGender) forControlEvents:UIControlEventValueChanged];
I created the method:
-(void)toggleGender{
NSLog(#"%d",self.seg.selectedSegmentIndex)
if (self.seg.selectedSegmentIndex == 0)
{
NSLog(#"User is a man");
} else if (self.seg.selectedSegmentIndex == 1)
{ NSLog(#"User is a female");
}
}
The first NSLog prints -1 as value if the value has changed.
I declared seg in the header file, and created a property and synthesized it!
I am currently NOT dealllocing this object. (I know this is a memory leak)
Why am I unable to read-out the selected segment, and why does it give back -1 as selected item?
Try this :
self.seg = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:#"Man",#"Female", nil]];
[self.seg addTarget:self action:#selector(toggleGender:) forControlEvents:UIControlEventValueChanged];
(NOTE THE ':' in the #selector..). And change your function by this :
-(void)toggleGender:(id)sender
{
if ([sender isKindOfClass:[UISegmentedControl class]])
{
NSLog(#"%d",[(UISegmentedControl *)sender selectedSegmentIndex]);
if ([(UISegmentedControl *)sender selectedSegmentIndex] == 0)
{
NSLog(#"User is a man");
}
else if ([(UISegmentedControl *)sender selectedSegmentIndex] == 1)
{
NSLog(#"User is a female");
}
}
}

iPhone: Button Level

i'm new to iPhone.Is there any way to set 3 images to button, and when I will click the button it will change button image in a circle ? Thanks...
In your IBAction method for the button click, you can keep a count of which image you are on, and also have all 3 UIImages ready to go (so there isn't the load everytime..unless the images are large (which they shouldn't be for a button) and check it and rotate it basically with something simple like (assumes you have 3 instance variables of UIImages ready that are already initialized with the images.:
-(IBAction) myButtonPress:(id)sender {
int imageCounter = 0;
if(imageCounter == 0) {
[myButtonImage setBackgroundImage:image1 forState:UIControlStateNormal];
imageCounter++;
}
else if(imageCounter == 1) {
[myButtonImage setBackgroundImage:image2 forState:UIControlStateNormal];
imageCounter++;
}
else if(imageCounter == 2) {
[myButtonImage setBackgroundImage:image3 forState:UIControlStateNormal];
imageCounter = 0;
}
//Do other button press stuff
}