参数化UIButton动作选择器在另一个类中的方法? [英] Parameterized UIButton action selector for method in another class?

查看:161
本文介绍了参数化UIButton动作选择器在另一个类中的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这在很大程度上是一个句法问题。如何设置UIButton操作选择器来调用不同类的方法?我做了一个#import类的方法,我需要使用按钮调用,我有以下部分了解按钮代码应该是什么样子:

  UIButton * btnSplash = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
btnSplash.frame = CGRectMake(250,270,180,30);
[btnSplash setTitle:@MenuforState:UIControlStateNormal];
[btnSplash addTarget:self action:@selector([CLASS METHOD:PARAMETER])forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btnSplash];但是,我收到以下错误:


预期':'before'[@ token



@selector中缺少方法名




我在参考库中看到的示例代码调用了本地方法,所以我试图泛化,我的尝试迄今为止都是无效的。



感谢

解决方案

选择器是方法名称的表示,无论哪些类别或类别



假设你有一个类 AnotherClass ,它实现了方法 - )doSomething:(id)sender 。对应的选择器是 doSomething:,在代码中表示为 @selector(doSomething:)。如果你想要按钮动作来调用该方法,你需要有一个 AnotherClass 的实例,并且这个实例是动作 target ,而不是 self 。因此你的代码应该有:

  #importAnotherClass.h

AnotherClass * instanceOfAnotherClass;
//将实例赋值给instanceOfAnotherClass

UIButton * btnSplash = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnSplash.frame = CGRectMake(250,270,180,30);
[btnSplash setTitle:@MenuforState:UIControlStateNormal];

[btnSplash addTarget:instanceOfAnotherClass
action:@selector(doSomething :)
forControlEvents:UIControlEventTouchUpInside];

[self addSubview:btnSplash];


This is largely a syntactical question. How does one set the UIButton action selector to call a method of a different class? I've done a #import of the class whose methods I need to call with the button and I have the following partial understanding of what the button code should look like:

    UIButton *btnSplash = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btnSplash.frame = CGRectMake(250, 270, 180, 30);
    [btnSplash setTitle:@"Menu" forState:UIControlStateNormal];
    [btnSplash addTarget:self action:@selector([CLASS METHOD:PARAMETER]) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btnSplash];

However, I get the following errors:

expected ':' before '[' token

method name missing in @selector

The sample code I've seen in the reference library calls local methods, so I'm trying to generalize and my attempts have thus far been unfruitful.

Thanks

解决方案

A selector is a representation of a method name regardless of which classes or categories implement it.

Say you have a class called AnotherClass which implements the method - (void)doSomething:(id)sender. The corresponding selector is doSomething:, represented in code as @selector(doSomething:). If you want the button action to invoke that method, you need to have an instance of AnotherClass — and it is this instance that is the action target, instead of self. Hence your code should have:

#import "AnotherClass.h"

AnotherClass *instanceOfAnotherClass;
// assign an instance to instanceOfAnotherClass

UIButton *btnSplash = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnSplash.frame = CGRectMake(250, 270, 180, 30);
[btnSplash setTitle:@"Menu" forState:UIControlStateNormal];

[btnSplash addTarget:instanceOfAnotherClass
              action:@selector(doSomething:)
    forControlEvents:UIControlEventTouchUpInside];

[self addSubview:btnSplash];

这篇关于参数化UIButton动作选择器在另一个类中的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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