Different callOut in annotationView - iphone

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
}

Related

how to create id sender button inside button in iphone

I am new to iPhone programming.I have taken one button inside that, I have created another button programmatically with id sender ,I want to use multiple buttons inside that id sender button using tags.Now I did some coding but it is showing some Exception.When i am giving [self somemethodname:(id)sender]; here I am getting problem, actually [self playOrPause] this one I have used in another button.How to slove this please any body tell me.
Thanks
- (void)playOrPause
{
UIButton *aa = [UIButton buttonWithType:UIButtonTypeCustom];
[aa addTarget:self action:#selector(play:)forControlEvents:UIControlEventTouchUpInside];
NSLog(#"hi iam 2");
[self play:(id)sender];
}
-(void)play:(id)sender
{
UIButton *instanceButton = (UIButton*)sender;
instanceButton.tag++;
if (instanceButton.tag == 1)
{
NSLog(#"hi");
} else if (instanceButton.tag == 2)
{
NSLog(#"hi2");
}
}

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);
}
}

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

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
}
}

how to find which objects are selected:TRUE from 64 buttons labelled b1, b2 etc

I have a project where there are 64 buttons, you have to click certain ones, then click 'done'. If you clicked the right ones, you get some points, and those then need to disappear.
I have let the user keep track of which buttons are pressed by doing sender setSelected:TRUE.
The first bit is all working fine, but I'd like to be able to then hide the buttons that were selected when the user clicked 'done'.
My current thoughts are:
- what I used in Actionscript to do the same thing was
for (i=1;i++;i<65) {
if(getProperty ("b"+ i, _visible) == true) {do blah blah blah} }
I'm really really really hoping there is an obvious equivalent in objective C that does the same thing?
I definitely do not want to have to go through all 64 buttons and type if ([b1 isSelected == TRUE]etc...
I can't just use sender, as it may be several buttons that have previously been selected that I need to access.
EDIT -
This is now the code that is called when user presses one of the 64 buttons.
-(IBAction) pressed:(id)sender {
UIButton *button = (UIButton *)sender;
if ([sender isSelected] ==FALSE) {
[sender setSelected:TRUE];
}
else {
[sender setSelected:FALSE];
}
if ([myArray containsObject:sender])
{
[myArray removeObject:sender];
}
else {
[myArray addObject:sender];
}
}
This is called when they press the 'done' button.
-(IBAction) checkTotal:(id)sender {
if (total == [(totaltxt.text) intValue]) {
score += 1;
scoretxt.text = [NSString stringWithFormat:#"%i", score];
for (UIButton *b in myArray)
{
[b setHidden:TRUE];
}
[myArray removeAllObjects];
}
else {
// some indication that they pressed the button but were wrong.
}
}
It unfortunately still won't hide the button.
It works if I change it to [n1 setHidden:TRUE] to hide the matching textbox above the button, but won't hide even a specific button -eg- [b1 setHidden:TRUE], let alone all the buttons in my array.
AAAAAAAARGH!!!!
Any ideas?
You can iterate on your view's subviews, and check whether the subview is a button:
for (UIView *view in self.view.subviews)
{
if ([view isKindOfClass: [UIButton class]])
{
// Do processing here
// For instance:
if ((UIButton *)view).isSelected)
view.hidden = YES;
}
}
If you dont want to iterate through all the buttons then how about store a reference to your button in an array then just iterate through that array and clear it when the use clicks done?
-(void)click:(id)sender
{
if([myArray containsObject:sender])
[myArray removeObject:sender];
else
[myArray addObject:sender];
}
-(void)doneClicked:(id)sender
{
for(UIButton *b in myArray)
{
[b setHidden:TRUE];
}
[myArray removeAllObjects]; //whateverr the method is i dont remember it off the top of my head
}
Try [b setAlpha:0.0];
Set the tag when creating the buttons and store the tag index in an array.
-(IBAction) pressed:(id)sender {
}
by sender.tag
then finally run the loop hide all object
and again run loop to unhide the object if it exists in array.

Creating a custom UIKeyBoard for iPhone

If anyone has the app GymBuddy, then they will know what I am talking about. They seem to use the stock Number Pad keyboard but have added a "." button in the lower left as well as a bar across the top to switch to alpha characters. Does anyone know how to do this? Do I make a new view like the keyboard and pull it up and have the buttons correspond to the textField for input? I can't seem to find any information on customizing a keyboard or creating your own. Thanks
I have done this. Basically you add your own button as a subview of the UIKeyboard like this:
// This function is called each time the keyboard is going to be shown
- (void)keyboardWillShow:(NSNotification *)note {
// Just used to reference windows of our application while we iterate though them
UIWindow* tempWindow;
// Because we cant get access to the UIKeyboard throught the SDK we will just use UIView.
// UIKeyboard is a subclass of UIView anyways
UIView* keyboard;
// Check each window in our application
for(int c = 0; c < [[[UIApplication sharedApplication] windows] count]; c ++)
{
// Get a reference of the current window
tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:c];
// Loop through all views in the current window
for(int i = 0; i < [tempWindow.subviews count]; i++)
{
// Get a reference to the current view
keyboard = [tempWindow.subviews objectAtIndex:i];
// From all the apps i have made, they keyboard view description always starts with <UIKeyboard so I did the following
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES)
{
// Only add the Decimal Button if the Keyboard showing is a number pad. (Set Manually through a BOOL)
if (numberPadShowing && [keyboard viewWithTag:123] == nil) {
// Set the Button Type.
dot = [UIButton buttonWithType:UIButtonTypeCustom];
// Position the button - I found these numbers align fine (0, 0 = top left of keyboard)
dot.frame = CGRectMake(0, 163, 106, 53);
dot.tag = 123;
// Add images to our button so that it looks just like a native UI Element.
[dot setImage:[UIImage imageNamed:#"dotNormal.png"] forState:UIControlStateNormal];
[dot setImage:[UIImage imageNamed:#"dotHighlighted.png"] forState:UIControlStateHighlighted];
//Add the button to the keyboard
[keyboard addSubview:dot];
// When the decimal button is pressed, we send a message to ourself (the AppDelegate) which will then post a notification that will then append a decimal in the UITextField in the Appropriate View Controller.
[dot addTarget:self action:#selector(sendDecimal:) forControlEvents:UIControlEventTouchUpInside];
return;
}
else if (numberPadShowing && [keyboard viewWithTag:123])
{
[keyboard bringSubviewToFront:dot];
}
else if (!numberPadShowing)
{
for (UIView *v in [keyboard subviews]){
if ([v tag]==123)
[v removeFromSuperview];
}
}
}
}
}
}
- (void)sendDecimal:(id)sender {
// The decimal was pressed
}
Hope that's clear.
-Oscar
Check this post, this could be your answer:
UIKeyboardTypeNumberPad and the missing "return" key