gpt4 book ai didi

ios - 在 objective-c 中创建 url 方法的最佳方法

转载 作者:行者123 更新时间:2023-11-28 22:23:39 25 4
gpt4 key购买 nike

我的应用中有两个仅包含 URL 的标签:

-(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];

并调用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]];
}

关于ios - 在 objective-c 中创建 url 方法的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19602791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com