ios 将参数传递给选择器操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16155616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:17:37  来源:igfitidea点击:

Passing Parameters to Selector action

iosobjective-cstringparametersselector

提问by jsetting32

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

我处于一种我认为我接近解决的情况,但也许不是。

I have a tableview of custom tableview cells.

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

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

目前每个 tableviewcell 有 3 个 uibuttons,访问它们并赋予它们功能是没有问题的。

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.

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

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 :/.

也许我的问题措辞不好:/。

Any help will be great :)

任何帮助都会很棒:)

EDIT: 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?

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

回答by dmee3

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:

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

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

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

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

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.

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

Hope that helps.

希望有帮助。

回答by nsgulliver

You just need :when passing parameter to a selector if you need to pass one parameter.

:如果您需要传递一个参数,则只需在将参数传递给选择器时需要。

[cell.buyTicketOutlet addTarget:self action:@selector(pushToTicketURL:) forControlEvents:UIControlEventTouchUpInside];

Basically :means you are passing an arguement to the selector and on the receiving end you could define your selector method with the object.

基本上:意味着您正在将参数传递给选择器,并且在接收端您可以使用对象定义选择器方法。

回答by jsetting32

Refering to fDmitry's response,

参考 fDmitry 的回应,

Here's the full code I used for his explanation. It works phenomally :). Thank you to the Omar Abdelhafith, whose link helped me, and fDmitry for getting me on the right track :)

这是我用于他的解释的完整代码。它非常有效:)。感谢 Omar Abdelhafith,他的链接帮助了我,感谢 fDmitry 让我走上正轨:)

-(void)pushToTicketURL:(id)sender{
    UIButton *button = (UIButton*)sender;
    UIView *view = button.superview; //Cell contentView
    SearchTableCell *cell = (SearchTableCell *)view.superview;
    NSLog(@"%@",cell.EventTitle.text); //Cell Text

    UIViewController *webViewController = [[UIViewController alloc] init];
    webViewController.title = cell.EventTitle.text;
    [webViewController.title sizeWithFont:10];
    UIWebView *uiWebView = [[UIWebView alloc] initWithFrame: CGRectMake(0,0,320,370)];

    [uiWebView loadRequest:[NSURLRequest requestWithURL: [NSURL URLWithString:cell.EventTicketURL]]];

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

    [self.navigationController pushViewController:webViewController animated:YES];
}

This snippet of code helped me so much and hope it helps anyone else whose falls into the same problem :).

这段代码对我帮助很大,希望它可以帮助其他遇到同样问题的人:)。

回答by Rob Napier

@fDmitry's approach isn't bad, and but there are better ways in modern ObjC. Using associated objects, you can attach arbitrary objects (i.e. your URL) to other objects (i.e. your button). You can the ask the sender (the button) for the URL and process it. Here's one way to do it:

@fDmitry 的方法还不错,但现代 ObjC 中有更好的方法。使用关联对象,您可以将任意对象(即您的 URL)附加到其他对象(即您的按钮)。您可以向发件人(按钮)询问 URL 并对其进行处理。这是一种方法:

// Create a category. The "MY" is just a prefix for your namespace
@interface UIButton (MYURLAdditions)
@property (nonatomic, readwrite, strong) NSURL *MYURL;
@end

// Add the information with a associated object

#import <objc/runtime.h>
@implementation (MYURLAdditions)

static char MYURLKey;
- (NSURL *)MYURL {
  return objc_getAssociatedObject(self, &MYURLKey);
}

- (void)setMYURL:(NSURL *)URL {
  objc_setAssociatedObject(obj, &MYURLKey, URL, OBJC_ASSOCIATION_RETAIN);
}

With that, after importing your category, you can call [button setMYURL:]to set the URL on the button. You can then use [sender MYURL]to fetch it in the IBAction.

这样,在导入类别后,您可以调用[button setMYURL:]以设置按钮上的 URL。然后您可以使用[sender MYURL]它在 IBAction 中获取它。