Ios 在objective c中创建url方法的最佳方法
我的应用程序中有两个标签纯粹包含URL:Ios 在objective c中创建url方法的最佳方法,ios,methods,uitapgesturerecognizer,openurl,Ios,Methods,Uitapgesturerecognizer,Openurl,我的应用程序中有两个标签纯粹包含URL: -(void)openURLA{ NSString *url = @"http://urla.com"; [[UIApplication sharedApplication] openURL: [NSURL URLWithString:url]]; } -(void)openURLB{ NSString *url = @"http://urlb.com"; [[UIApplication sharedApplication
-(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]];
}
在现有方法中包含以下代码:
UITapGestureRecognizer *gra = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openURLA)];
UITapGestureRecognizer *grb = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openURLB)];
当用户点击其中一个标签时,openURL方法运行良好,URL在safari中打开
我想知道如何创建一个方法来打开URL并传递包含label1.text或label2.text值的参数
我无法100%确定从何处开始执行此操作,因此我将感谢您的帮助。编辑: 遵循以下全部代码:
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];
和调用UITapGestureRecognitor的方法
- (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]];
}
在创建标签集标记时,如:
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];
并使用以下代码查找它被点击的视图
- (void)openURLS:(UITapGestureRecognizer*)sender
{
UIView *view = sender.view;
int tag = view.tag;
if (tag == 1000) {
...
}
}
这是[手势视图],请更改它。然后你有两个标签,所以把标签放在你的标签上。根据标签访问,..视图
是已添加识别器的视图。如果将其添加到两个标签的公共superview中,则不会出现此问题work@GabrielePetronella-谢谢你让我成真。。检查我编辑的答案:)@TamilKing-谢谢你让我成为现实。。检查我编辑的答案:)@iPatel-为什么使用两次轻触手势。我想一个就足够了。我可以将标签设置为URL吗?因为这个应用程序从数据库获取URL,并且所有内容都是动态的,所以我必须使用一个方法来传递label.text值。但是你的答案很有用,我会把它放在书签里!非常感谢:)