Best way to create a url method in objective c - ios

I have two labels in my app which purely contain URLs:
-(void)openURLA{
NSString *url = #"http://urla.com";
[[UIApplication sharedApplication] openURL: [NSURL URLWithString:url]];
}
-(void)openURLB{
NSString *url = #"http://urlb.com";
[[UIApplication sharedApplication] openURL: [NSURL URLWithString:url]];
}
And this code inside an existing method:
UITapGestureRecognizer *gra = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(openURLA)];
UITapGestureRecognizer *grb = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(openURLB)];
When a user taps on one of these labels, the openURL methods run fine and the URL opens in safari.
I was wondering how I could create just one method that will open the URL and pass an argument containing label1.text or label2.text values?
I am not 100% sure where to begin doing this so I will appreciate some help.

EDITED:
Follow this whole code:
UILabel * label1 = [[UILabel alloc] initWithFrame:CGRectMake(40, 70, 300, 50)];
label1.backgroundColor = [UIColor redColor];
label1.userInteractionEnabled = YES;
label1.textColor=[UIColor whiteColor];
label1.text = #"http://urla.com";
[self.view addSubview:label1];
UILabel * label2 = [[UILabel alloc] initWithFrame:CGRectMake(40, 130, 300, 50)];
label2.backgroundColor = [UIColor redColor];
label2.userInteractionEnabled = YES;
label2.textColor=[UIColor whiteColor];
label2.text = #"http://urlb.com";
[self.view addSubview:label2];
UITapGestureRecognizer *gsture1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(openURLS:)];
[label1 addGestureRecognizer:gsture1];
UITapGestureRecognizer *gesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(openURLS:)];
[label2 addGestureRecognizer:gesture2];
And call method of UITapGestureRecognizer
- (void)openURLS:(UITapGestureRecognizer*)gesture
{
UILabel *lblUrl=(UILabel *)[gesture view];
NSLog(#"%#", lblUrl.text); // here you get your selected label text.
[[UIApplication sharedApplication] openURL: [NSURL URLWithString:lblUrl.text]];
}

On creating a label set tag like:
label1.tag = 1000;
label2.tag = 1001;
UITapGestureRecognizer *gsture1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(openURLS:)];
[label1 addGestureRecognizer:gsture1];
UITapGestureRecognizer *gesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(openURLS:)];
[label2 addGestureRecognizer:gesture2];
and use the following code to find the view it was tapped
- (void)openURLS:(UITapGestureRecognizer*)sender
{
UIView *view = sender.view;
int tag = view.tag;
if (tag == 1000) {
...
}
}

Related

iOS UITapGestureRecognizer not working sometimes

Sometimes the selector method "tagClickAtIndex" not getting called.
UILabel* label = [[UILabel alloc] init];
label.userInteractionEnabled = YES;
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tagClickAtIndex:)];
[label addGestureRecognizer:tapGesture];
-(void)tagClickAtIndex:(UITapGestureRecognizer*)gesture
{
NSMutableDictionary *mutDict = [[[NSMutableDictionary alloc]initWithDictionary:[_tagArray objectAtIndex:gesture.view.tag]] mutableCopy];
[mutDict setValue:[NSNumber numberWithLong:gesture.view.tag] forKey:#"index"];
[self.delegate tagClickAtIndex:mutDict];
}
you forget to set the frame of label, the reason it only define the clickable area
label.frame = CGRectMake(0,0,200,30)
[yourMainview addSubView:label]
and set if you need
tapGesture.numberOfTapsRequired = 1;
If you are programmatically creating UIlabel,your above code is not working.I tried your code.
UILabel* label = [[UILabel alloc] init];
label.userInteractionEnabled = YES;
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tagClickAtIndex:)];
[label addGestureRecognizer:tapGesture];
It does not show anything.As you have not set the frame and add label view,it does not show.
So Then I tried creating UILabel freshly.I set frame,addSubView to self.view and I set the color as well as number of taps for label.
UILabel* label = [[UILabel alloc] init];
label.frame = CGRectMake(30, 100, 200, 100);
label.text = #"click me";
label.textColor = [UIColor blueColor];
[self.view addSubview:label];
Now Tap gesture recognizer for label
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tagClickAtIndex:)];
tapGesture.numberOfTapsRequired = 1;
label.userInteractionEnabled = YES;
[label addGestureRecognizer:tapGesture];
in delegate method I put log,It calls everytime whenever I touch the label
-(void)tagClickAtIndex:(UITapGestureRecognizer*)gesture
{
NSLog(#"The tap is called");
.........
}
The printed statements shows
The tap is called

UIGestureRecognizer in objective-C++

I want to know the UIGestureRecognizer working or not in Objective-C++ because i've implemented this one but tap method never calling. So please let me know is it possible or not in Objective-C++.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
imgView.image = [UIImage imageNamed:#"dharm"];
[self.view addSubview:imgView];
imgView.backgroundColor = [UIColor redColor];
UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tap:)];
tapRecognizer.numberOfTapsRequired = 1;
[tapRecognizer setDelegate:self];
[imgView addGestureRecognizer:tapRecognizer];
}
- (void)tap:(id)sender {
NSLog(#"Tap Pressed");
}
Try adding [imgView setUserInteractionEnabled:YES];

How to handle event of code-created UIView

I have unknown amount of UIImageViews which are created in the code, not in the xib file and i need to handle the taps on those images. The handling of each of this image view is going to be the same. How do i do this?
Demo code for your implemantation
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImageView *imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapDetected:)];
singleTap.numberOfTapsRequired = 1;
[imgView1 setUserInteractionEnabled:YES];
[imgView1 addGestureRecognizer:singleTap];
imgView1.tag = 1;
imgView1.backgroundColor = [UIColor redColor];
[self.view addSubview:imgView1];
UIImageView *imgView2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 200, 100, 100)];
UITapGestureRecognizer *singleTap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapDetected:)];
singleTap.numberOfTapsRequired = 1;
[imgView2 setUserInteractionEnabled:YES];
[imgView2 addGestureRecognizer:singleTap2];
imgView2.tag = 2;
imgView2.backgroundColor = [UIColor blueColor];
[self.view addSubview:imgView2];
}
-(void)tapDetected:(UITapGestureRecognizer *)gestureRecognizer{
UIImageView *myImg = (UIImageView*)gestureRecognizer.view;
NSLog(#"tag : %ld",(long)myImg.tag);
}
Try this code to add a gesture recogniser
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(action:)];
[_imgview addGestureRecognizer:tap];
_imgview.userInteractionEnabled = YES;
You can add UITapGestureRecognizer to detect touch on UIImageView.
Just use below method with argument, your image view and a unique tag, and you're done !
- (void) setTapGestureOnImageView:(UIImageView *)imageView withTag:(NSInteger)tag {
//this is important, by default user interaction isn't enabled, we have to enable it.
imageView.userInteractionEnabled = YES;
//create a tap gesture (in example this is sinlge tap) with target and action to call when user tap
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(detectTap:)];
//you can customize taps too
//tap.numberOfTapsRequired = 2;
//tap.numberOfTouchesRequired = 2;
//add gesture on image view
[imageView addGestureRecognizer:tap];
}
- (void)detectTap:(UIGestureRecognizer *)recognizer {
//Get the tapped image view from recognizer
UIImageView *imageView = (UIImageView *)recognizer.view;
//Check for condition, which image view tapped
if(imageView.tag == 1) {
//do something 1st imageview
}
else if(imageView.tag == 2) {
//do something for 2nd imageview
}
else {
//do something else
}
}
May be this will be useful to you
- (void)viewDidLoad
{
NSArray *imagesArray = [NSArray arrayWithObjects:#"statement_card_1.png", #"statement_card_2.png", #"statement_card_3.png", #"statement_card_4.png", #"statement_card_5.png", nil];
short xPadding = 10;
for (int i = 0; i< imagesArray.count; i++)
{
UIImage *imageRecipe =[UIImage imageNamed:[imagesArray objectAtIndex:i]];
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(xPadding, xPadding+(i*60),imageRecipe.size.width, imageRecipe.size.height)];
imgView.tag =IMAGETAG + i;
[imgView setImage:imageRecipe];
imgView.userInteractionEnabled = YES;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapDetected:)];
[imgView addGestureRecognizer:singleTap];
[self.view addSubview:imgView];
}
}
-(void)tapDetected:(UITapGestureRecognizer *)recognizer
{
UIImageView *TempImg = (UIImageView *)recognizer.view;
NSLog(#"tag : %ld",(long)TempImg.tag);
}

Adding custom gesture to iCarousel items

Hi I've been working with Xcode for a while and used UIGestureRecognizers many times now. At this moment i'm developing an iPad app that needs a carousel in it, so what's better then using iCarousel as i've already encountered it and I think it's great.
I'm having some issues adding to the carousel items a UISwipeGestureRecognizer (I would use it to delete the "swiped" item). Would look easy but it just looks like the UIGestureRecognizer is not there, but if print view.recognizer I can see it is actually there. What am I missing?
And sorry for any language mistakes!
The code
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UILabel *nameLabel = nil;
UILabel *notesLabel = nil;
UIImageView *icon1 = nil;
UIImageView *icon2 = nil;
Notebook *referenceNotebook = [_notebookArray objectAtIndex:index];
//create new view if no view is available for recycling
if (view == nil)
{
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 250.0f, 373.0f)];
((UIImageView *)view).image = [UIImage imageNamed:#"notebookBase"];
view.contentMode = UIViewContentModeCenter;
CGPoint startingPoint = view.frame.origin;
CGSize startingSize = view.frame.size;
nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(startingPoint.x, startingPoint.y + 60, startingSize.width, 100)];
nameLabel.backgroundColor = [UIColor clearColor];
nameLabel.textAlignment = NSTextAlignmentCenter;
nameLabel.font = [FontController useFontKlinic_Light:50];
nameLabel.tag = 1;
[view addSubview:nameLabel];
notesLabel = [[UILabel alloc]initWithFrame:CGRectMake(startingPoint.x, startingPoint.y + 95, startingSize.width, 100)];
notesLabel.backgroundColor = [UIColor clearColor];
notesLabel.textAlignment = NSTextAlignmentCenter;
notesLabel.font = [FontController useFontKlinic_Light:30];
[view addSubview:notesLabel];
icon1 = [[UIImageView alloc] initWithFrame:CGRectMake(startingPoint.x + 105, startingPoint.y + 18, 40, 40)];
icon1.image = [UIImage imageNamed:#"notebookIcon1"];
[view addSubview:icon1];
icon2 = [[UIImageView alloc] initWithFrame:CGRectMake(startingPoint.x + 105, startingPoint.y + 320, 40, 40)];
icon2.image = [UIImage imageNamed:#"notebookIcon1"];
[view addSubview:icon2];
//UIView *swipeView = [[UIView alloc] initWithFrame:CGRectMake(startingPoint.x, startingPoint.y, startingSize.width, startingSize.height)];
//swipeView.backgroundColor = [UIColor clearColor];
//UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(carouselSwipewipeHandler:)];
//[swipeRecognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
// [swipeView addGestureRecognizer:swipeRecognizer];
//[view addSubview:swipeView];
}
else
{
//get a reference to the label in the recycled view
nameLabel = (UILabel *)[view viewWithTag:1];
}
nameLabel.text = referenceNotebook.name;
notesLabel.text = #"n note";
UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(carouselSwipewipeHandler:)];
[swipeRecognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
[view addGestureRecognizer:swipeRecognizer];
view.tag = index;
NSLog(#"gesture recogs: %#", view.gestureRecognizers);
return view;
}
Anyone with an idea of what I am doing wrong?
Here's also a link to the iCarousel documentation:
https://github.com/nicklockwood/iCarousel#detecting-taps-on-item-views Thanks in advance!
Only one gesture can be active at any one time by default, and the carousel uses a gesture internally for scrolling. So, if you want to use other gestures you need to add a delegate to your gesture and the the carousel gesture so that you can make them work simultaneously.

(WeeLoader) 'UITapGestureRecognizer' was not declared in this scope

I know that when the error is "'blah' was not declared in this scope" means when the object was not created correctly, but when I am making a Notification Center widget for iOS, using the WeeLoader template and THEOS to compile, I am getting this error: 'UITapGestureRecognizer' was not declared in this scope.
Here is my .mm file:
- (void)loadFullView {
UITapGestureRecognizer *Tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
UIImage *bg = [[UIImage imageWithContentsOfFile:#"/System/Library/WeeAppPlugins/WeeAppTest.bundle/WeeAppBackground.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:71];
UIImageView *bgView = [[UIImageView alloc] initWithImage:bg];
bgView.frame = CGRectMake(0, 0, 316, 71);
bgView.userInteractionEnabled = YES;
[bgView addGestureRecognizer:Tap];
[_view addSubview:bgView];
[bgView release];
[Tap release];
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 316, 71)];
lbl.backgroundColor = [UIColor clearColor];
lbl.textColor = [UIColor whiteColor];
lbl.text = #"Hello world";
lbl.textAlignment = UITextAlignmentCenter;
[_view addSubview:lbl];
[lbl release];
}
-(void) handleTapGesture:(UIGestureRecognizer *) sender {
}
What am I doing wrong? I am pretty sure that I am declaring everything correctly, by the way, the code does work with out the UITapGestureRecognizer.
Thank you.
I think there are a few things going on here.
I think you could be getting the out of scope error because the gesture recognizer should be declared in the viewDidLoad or the init methods.
Also, in the UITapGestureRecognizer *Tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)]; You call your method handleSingleTap:
Therefore your method should be -(void) handleSingleTap:(UITapGestureRecognizer *) sender

Resources