将参数传递给Selector操作 [英] Passing Parameters to Selector action

查看:117
本文介绍了将参数传递给Selector操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我处在一种情况,我认为我即将解决,但也许不是。

I am in a situation where I think I am close to solving, but maybe not.

我有自定义tableview单元格的tableview。

I have a tableview of custom tableview cells.

目前每个tableviewcell有3个uibutton,访问它们并给出他们的功能没有问题。

Currently each tableviewcell has 3 uibuttons, accessing them and giving them functionality is no problem.

我遇到问题的3个按钮之一。该按钮将用户从当前导航控制器推送到webview控制器,但我希望webview控制器成为当前tableview单元格所持有的http地址。

One of the 3 buttons I'm having problems with. The button pushes the user from the current navigation controller to a webview controller, but I want the webview controller to be the http address the current tableview cell is holding.

Heres a我正在使用的代码的小snippit。

Heres a small snippit of the code I'm using.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Identifier";

    SearchTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"SearchTableCell" owner:nil options:nil];
        //cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
        for(id currentObject in topLevelObjects)
        {
            if([currentObject isKindOfClass:[SearchTableCell class]])
            {
                cell = (SearchTableCell *)currentObject;
                break;
            }
        }
    }


    Player *event = [_party.events objectAtIndex:indexPath.row];
    cell.EventTitle.text = event.name;
    cell.EventDescription.text = event.description;
    cell.EventStartTime.text = event.start_date;
    cell.EventEndTime.text = event.end_date;
    cell.EventTicketURL = event.ticketURL;

    NSString *string = event.ticketURL;

    [cell.buyTicketOutlet addTarget:self action:@selector(pushToTicketURL:string) forControlEvents:UIControlEventTouchUpInside];
    //[cell.AddEventOutlet addTarget:self action:@selector(:) forControlEvents:UIControlEventTouchUpInside];
    //cell.textLabel.numberOfLines = 2;
    return cell;
}




- (void) pushToTicketURL:(NSString *)string
{

    UIViewController *webViewController = [[UIViewController alloc] init];
    webViewController.title = @"Tickets";
    UIWebView *uiWebView = [[UIWebView alloc] initWithFrame: CGRectMake(0,0,320,370)];

    [uiWebView loadRequest:[NSURLRequest requestWithURL: [NSURL URLWithString:string]]];

    [webViewController.view addSubview:uiWebView];
    [uiWebView release];

    [self.navigationController pushViewController:webViewController animated:YES];
    NSLog(@"Button pressed");
}

我在同一个问题上看过其他一些评论,但就像我说的那样,我只需要将一个参数传递给选择器,我明白这是不可能的,但是我可以用其他方法解决这个问题?

I have seen some other reviews on the same question but like I said, I just need to pass one parameter to a selector, I understand that is not possible, but what other ways can I go about this problem?

也许我的问题措辞严厉:/。

Maybe my question is badly worded :/.

任何帮助都会很棒:)

编辑:我理解 [cell。 buyTicketOutlet addTarget:self action:@selector(pushToTicketURL:string)forControlEvents:UIControlEventTouchUpInside]; 给出错误。但我可以编写什么样的代码来做我想做的事情?

I understand [cell.buyTicketOutlet addTarget:self action:@selector(pushToTicketURL:string) forControlEvents:UIControlEventTouchUpInside]; gives errors. But what work around can I code to do what I want?

推荐答案

虽然你不能直接将URL传递给选择器,你可以传递按钮本身。所以我的建议是根据你的单元格行标记按钮并创建这样的动作:

While you can not pass the URL directly to the selector, you can pass the button itself. So my suggestion would be to tag button according to your cell row and create action like this:

void goToAddress: (id) sender {
   UIButton *button = (UIButton*) sender;
   if (button.tag == ..) {
   ...
  }
}

然后,在该方法中,您可以从单元格中查找URL,其数字等于按钮的标记。

Then, in that method you can "look up" URL from the cell with the number equal to tag of the button.

除此之外,我很确定在按钮的选择器中传递自定义参数或多个参数是不可能的。如果我错了,请纠正我。

Other than that, I am fairly sure it's impossible to pass custom parameters or more than one parameter in button's selector. Please correct me if I'm wrong.

希望有所帮助。

这篇关于将参数传递给Selector操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆