【问题标题】:Best way to create a url method in objective c在目标 c 中创建 url 方法的最佳方法
【发布时间】:2013-10-26 04:24:36
【问题描述】:

我的应用中有两个完全包含 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% 确定从哪里开始,所以我将不胜感激。

【问题讨论】:

    标签: ios methods uitapgesturerecognizer openurl


    【解决方案1】:

    已编辑:

    按照整个代码:

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

    【讨论】:

    • 那个是【手势视图】请改一下。然后你有两个标签,所以为你的 UILable 放置标签。根据标签访问..
    • view 是识别器已添加到的视图。如果加在两个标签的公共superview上,这个就不行了
    • @GabrielePetronella - 谢谢你让我变得真实......检查我编辑的答案:)
    • @TamilKing - 谢谢你让我变得真实......检查我编辑的答案:)
    • @iPatel - 为什么你使用两次点击手势。我认为一个就足够了
    【解决方案2】:

    关于创建标签集标签,如:

        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) {
    ...
    }
    }
    

    【讨论】:

    • 我可以将标签设置为网址吗?
    • 因为这个应用程序从数据库中获取 URL,并且一切都是动态的,所以我不得不使用一种方法来传递 label.text 值。但是你的回答非常有用,我会保留这个书签!非常感谢:)
    猜你喜欢
    • 2013-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-01
    相关资源
    最近更新 更多