UIButton as a subview in MKAnnotationView doesn't work - objective-c

I have added a button to my annotation view, and it appears correctly. When the button is tapped, I want a call to be made, but when I tap the button, it doesn't respond (the button doesn't even highlight). It seems that the button doesn't receive the touch event, but I don't know why.
Here is my code in customAnnotationView.m
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
NSLog(#"initWithAnnotation start");
//initialze the view
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if (self != nil)
{
CGRect frame = self.frame;
frame.size = CGSizeMake(60.0, 85.0);
self.frame = frame;
self.backgroundColor = [UIColor clearColor];
self.centerOffset = CGPointMake(30.0, 42.0);
}
//this has nothing to do with my question
self.title=[[[NSString alloc] init] autorelease];
self.description=[[[NSString alloc] init] autorelease];
self.phoneNumber=[[[NSString alloc] init] autorelease];
customAnnotation * tempCust = (customAnnotation *)annotation;
self.title=annotation.title;
self.description=tempCust.description;
self.phoneNumber=tempCust.phoneNumber;
//here is the question button
self.userInteractionEnabled=YES;
self.exclusiveTouch=NO;
UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Call" forState:UIControlStateNormal];
//decide the button's position
CGRect rect=button.frame;
rect.origin.x=15;
rect.origin.y=47;
rect.size.width=40;
rect.size.height=20;
button.frame=rect;
[self addSubview:button];
[button becomeFirstResponder];
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
NSLog(#"initWithAnnotation finished");
return self;
}
//action method
-(void)buttonClicked:(id)sender{
NSString *callNumber=[[[NSString alloc] initWithFormat:#"tel://%#",self.phoneNumber] autorelease];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:callNumber]];
}
Why doesn't the button work?

self.rightCalloutAccessoryView = button;
Use the above instead of [self addSubview:button];
Hope this help's you

Related

i need a popupview when clicking button and again clicking i want to disapper that view in objective c

Here is the code that I am using for popup:
-(void)popup:(id)sender
{
//button action
UIView *popView=[[UIView alloc]initWithFrame:CGRectMake(Screen_Width-200,40, self.view.frame.size.width-130, self.view.frame.size.height/2-60)];
[popView setBackgroundColor:[UIColor redColor]];
popView.userInteractionEnabled = YES;
[self.view addSubview: popView];
}
You can do something like this in your viewDidLoad. So it's hidden bydefault.
UIView *popView=[[UIView alloc]initWithFrame:CGRectMake(Screen_Width-200,40, self.view.frame.size.width-130, self.view.frame.size.height/2-60)];
[popView setBackgroundColor:[UIColor redColor]];
popView.hidden = YES;
popView.userInteractionEnabled = YES;
[self.view addSubview: popView];
On your button click show it like,
popView.hidden = NO;
And take UITapGestureRecognizer on main View or root view and on touches call,
popView.hidden = YES;
Exaample of TapGestureRecognizer
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleSingleTap:)];
[self.view addGestureRecognizer:singleFingerTap];
[self.view setUserInteractionEnabled:YES];
//The event handling method
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
popView.hidden = YES;
}
Hope this will help :)

Calling an IBAction in code and sending a tag through too

So I have an IBAction hooked up to multiple buttons in IB I want this action to be fired when another IBAction is called I also want it to recognise an int from the second action as the [sender tag].
// viewController.m
-(IBAction)buttonPress {
int getInter = [sender tag];
UIView *tmpView = [self.view viewWithTag:getInter];
NSString *title = [(UIButton *)sender currentTitle];
}
I then have another action that needs to call buttonPress.
-(IBAction)secondButtonPress {
int varInt = 1
[self buttonPress: nil] <--- How do I pass varInt that buttonPress would recognise as a sender Tag here?
}
I realise that I could just create the button like so and duplicate the code in secondButtonPress but that seems to messy...
UIButton *tmpButton2 = (UIButton *)[self.view viewWithTag:varInt];
So the question is, is there a way of tricking buttonPress into thinking it has been pressed and passing a variable through as the sender tag for that action. In a way programatically tricking buttonPress into thinking it has been pressed.
Thank you in advance.
I ended up hooking it up to the same IBAction and running an if statement to do something if that tag was called.
Seems simple now!
-(IBAction)buttonPress {
int getInter = [sender tag];
if ([sender tag] == 2) {
//Do something just for that button.
}
else
{
UIView *tmpView = [self.view viewWithTag:getInter];
NSString *title = [(UIButton *)sender currentTitle];
}
}
UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 20, 20)];
[btn1 setTitle:#"Button 1" forState:UIControlStateNormal];
btn1.tag = 1;
[btn1 addTarget:self action:#selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
UIButton *btn2 = [[UIButton alloc] initWithFrame:CGRectMake(10, 50, 20, 20)];
[btn2 setTitle:#"Button 2" forState:UIControlStateNormal];
btn2.tag = 2;
[btn2 addTarget:self action:#selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn2];
UIButton *btn3 = [[UIButton alloc] initWithFrame:CGRectMake(10, 90, 20, 20)];
[btn3 setTitle:#"Button 3" forState:UIControlStateNormal];
btn3.tag = 3;
[btn3 addTarget:self action:#selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn3];
write button click method as following and get button tag in it..
-(IBAction)buttonClick:(id)sender {
UIButton *btn = (UIButton *) sender;
NSLog(#"%d",btn.tag);
}
Just set the tag in initWithNibName method. Follow the sample and see whether it is helpful for you!!
#property(nonatomic,strong)IBOutlet UIButton *btn1;
#property(nonatomic,strong)IBOutlet UIButton *btn2;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Set the Tag
self.btn1.tag=1;
self.btn1.tag=2;
}
return self;
}
-(IBAction)selectedButtonClick:(id)sender {
UIButton *btn = (UIButton *) sender;
NSLog(#"selectedButton%d",btn.tag);
}

is it Possible to pass parameter in UIView Class

I ve got a view controller . when i pass an extra object I m getting this error #No visible interface in the below function .could u help me out.below is the code.where i am passing an object of type photo ..
ItemImageView *itemImage = [[ItemImageView alloc]initWithFrame:CGRectMake(currentPhotoPositionX,0,pageButtonWidth,pageButtonHeight) andPhoto:photo];
#ItemImageview
- (id)initWithFrame:(CGRect)frame :andPhoto:(Photo *)photo
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
//Create Activity Indicator
activityIndicator= [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(225, 115, 30, 30)];
[activityIndicator setBackgroundColor:[UIColor clearColor]];
[activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
[self addSubview:activityIndicator];
//Create Label Comments
lblComments = [[UILabel alloc] initWithFrame:CGRectMake(30.0f, 100.0f, 60.0f, 43.0f)];
[lblComments setText:#"Comments"];
[lblComments setBackgroundColor:[UIColor clearColor]];
[self addSubview:lblComments];
//Create Label Likes
lblLikes = [[UILabel alloc] initWithFrame:CGRectMake(100.0f, 100.0f, 60.0f, 43.0f)];
[lblLikes setText:#"Likes"];
[lblLikes setBackgroundColor:[UIColor clearColor]];
[self addSubview:lblLikes];
//Create Item Button
btnItem = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnItem addTarget:self
action:#selector(:)
forControlEvents:UIControlEventTouchUpInside];
[btnItem setBackgroundImage:[UIImage imageNamed:#"1.png"] forState:UIControlStateNormal];
btnItem.frame = CGRectMake(0.0f, 0.0f, 144.0f, 143.0f);
[self addSubview:btnItem];
}
return self;
}
Change it into:
- (id)initWithFrame:(CGRect)frame andPhoto:(Photo *)photo
And also remember to add it to your .h file.

Issues with UISplitViewController and modal UIViewControllers

I have a really strange UI glitch in my iPhone/iPad app. Because I wanted to find the cause of it, I created a new project with as little code as possible. The important code is below.
Basically, I have a UISplitViewController containing two UIViewController subclasses. When a button is tapped, the first one presents modally and as UIModalPresentationFormSheet a UIViewController subclass called Modal. In there, when a button is tapped, another UIViewController subclass called Text is presented, this time as UIModalPresentationFullScreen. In Text, there is a UITextView. When it is tapped, everything is alright, but when the iPad is rotated, I get this:
The white part is the Text view controller, the red part in the background is ViewControllerTwo.
Does anybody have any idea why this happens? And what I can do to fix it?
Here is the Project: MediaFire
Here is the relevant source code:
// AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ViewControllerOne *one = [[ViewControllerOne alloc] init];
ViewControllerTwo *two = [[ViewControllerTwo alloc] init];
UISplitViewController *split = [[UISplitViewController alloc] init];
split.viewControllers = [NSArray arrayWithObjects:one, two, nil];
self.window.rootViewController = split;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
// ViewControllerOne.m
#import "Modal.h"
#implementation ViewControllerOne
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blueColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(30, 30, 44, 44);
[button addTarget:self action:#selector(buttonTapped) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonTapped
{
Modal *modalOne = [[Modal alloc] init];
modalOne.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:modalOne animated:YES];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
// Modal.m
#import "Text.h"
#implementation Modal
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *buttonOne = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonOne.frame = CGRectMake(30, 30, 44, 44);
buttonOne.tag = 1;
[buttonOne addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
UIButton *buttonTwo = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonTwo.frame = CGRectMake(30, 100, 44, 44);
buttonTwo.tag = 2;
[buttonTwo addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonOne];
[self.view addSubview:buttonTwo];
}
- (void)buttonTapped:(UIButton *)button
{
if (button.tag == 1)
{
Text *text = [[Text alloc] init];
text.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentModalViewController:text animated:YES];
}
else
{
[self dismissModalViewControllerAnimated:YES];
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
// Text.m
#implementation Text
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
UITextView *textView = [[UITextView alloc] initWithFrame:self.view.bounds];
textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:textView];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
You may need to present the modal views from the UISplitViewController instead of it's subviews...
UISplitViewController *splitViewController = [(AppDelegate *)[[UIApplication sharedApplication] delegate] splitViewController];
[splitViewController presentModalViewController:modal animated:YES];

UIbutton with longpress and Touchup inside

How to create a UIButton With two actions.
I know by using UILongPressGestureRecognizer we can perform Longpress.
But my requirement is,When I Long Press UIButton,it has to perform one action and when touch
up inside it, it has to perform another action.
Thanks.
Below is my code.
UIImage *redImage = [UIImage imageNamed:#"TabFav2.png"];
tabRedbutton = [UIButton buttonWithType:UIButtonTypeCustom];
[tabRedbutton setImage:redImage forState:UIControlStateNormal];
tabRedbutton.frame = CGRectMake(0.0, 0.0, 50,35);
redTAb = [[UIBarButtonItem alloc] initWithCustomView:tabRedbutton];
[tabRedbutton addTarget:self action:#selector(redbottonmethod) forControlEvents:UIControlEventTouchUpInside];
longpressGesture1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressHandler:)];
longpressGesture1.minimumPressDuration =0.1;
[longpressGesture1 setDelegate:self];
longpressGesture1.cancelsTouchesInView = NO;
[tabRedbutton addGestureRecognizer:longpressGesture1];
[longpressGesture1 release];
- (void)longPressHandler:(UILongPressGestureRecognizer *)gestureRecognizer {
if (longpressGesture.state == UIGestureRecognizerStateBegan)
{
NSlog(#"Long press");
}
}
-(void)redbottonmethod
{
NSlog(#"single tapped");
}
For the tap you can use UIButton's "addTarget:..." method and for the longpress you can add a gesture recognizer:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(100.0, 100.0, 100.0, 20.0);
[btn setTitle:#"Test" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(userTapped:) forControlEvents:UIControlEventTouchUpInside];
UILongPressGestureRecognizer *gr = [[UILongPressGestureRecognizer alloc] init];
[gr addTarget:self action:#selector(userLongPressed:)];
[btn addGestureRecognizer:gr];
[gr release];
[self.view addSubview:btn];
Of course you need to implement the 2 methods that will be called:
- (void)userTapped:(id)sender {
NSLog(#"user tapped");
}
- (void)userLongPressed:(id)sender {
NSLog(#"user long pressed");
}
Hope that helps.
=========
EDIT: It seems that you are using your button as a BarButtonItem inside a UIToolbar. So I changed my code to do the same:
- (void)viewDidLoad {
[super viewDidLoad];
// set up the button
UIImage *redImage = [UIImage imageNamed:#"TabFav2.png"];
UIButton *tabRedbutton = [UIButton buttonWithType:UIButtonTypeCustom];
tabRedbutton.backgroundColor = [UIColor redColor];
[tabRedbutton setImage:redImage forState:UIControlStateNormal];
tabRedbutton.frame = CGRectMake(0.0, 0.0, 50,35);
// set up a bar button item with the button as its view
UIBarButtonItem *redTab = [[UIBarButtonItem alloc] initWithCustomView:tabRedbutton];
// set up toolbar and add the button as a bar button item
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0, 100.0, 768.0, 40.0)];
toolbar.barStyle = UIBarStyleBlack;
NSArray *items = [NSArray arrayWithObject:redTab];
[toolbar setItems:items];
[self.view addSubview:toolbar];
[toolbar release];
// add tap handler to button for tap
[tabRedbutton addTarget:self action:#selector(redbottonmethod) forControlEvents:UIControlEventTouchUpInside];
// add gesture recognizer to button for longpress
UILongPressGestureRecognizer *longpressGesture1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressHandler:)];
longpressGesture1.minimumPressDuration =0.1;
[tabRedbutton addGestureRecognizer:longpressGesture1];
[longpressGesture1 release];
}
And the two methods that get called:
- (void)longPressHandler:(UILongPressGestureRecognizer *)gestureRecognizer {
NSLog(#"Long press");
}
-(void)redbottonmethod {
NSLog(#"single tapped");
}
This code definitely works.
By the way: I noticed that in your code in the 2 methods that get called you have typo: You must use NSLog() and not NSlog(). Could that be the problem?