0

アプリには、純粋に URL を含む 2 つのラベルがあります。

-(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 がサファリで開きます。

URL を開き、label1.text または label2.text 値を含む引数を渡すメソッドを 1 つだけ作成するにはどうすればよいでしょうか?

どこから始めればよいか 100% 確信が持てないので、助けていただければ幸いです。

4

2 に答える 2

1

次のようなラベル セット タグを作成する場合:

    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) {
...
}
}
于 2013-10-26T12:10:28.517 に答える
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]];
}
于 2013-10-26T04:35:08.033 に答える