如何在objective-c实例方法中传递字符串?
您好,我有多个 UIButtons(foo
和 bar
),按下时每个按钮都会调用不同的实例方法(doSomethingFoo
和 doSomethingBar
>)。这是我正在工作的代码:
CGRect fooImageRect = CGRectMake(38.0f, 192.0f, 130.0f, 25.0f);
UIButton *buttonFoo = [[UIButton alloc] init];
buttonFoo.frame = fooImageRect;
[buttonFoo setImage:[UIImage imageNamed:@"button_foo_130x25.png"] forState:UIControlStateNormal];
[buttonFoo addTarget:self action:@selector(doSomethingFoo:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonFoo];
[buttonFoo release];
CGRect barImageRect = CGRectMake(172.0f, 192.0f, 130.0f, 25.0f);
UIButton *buttonBar = [[UIButton alloc] init];
buttonBar.frame = barImageRect;
[buttonBar setImage:[UIImage imageNamed:@"button_bar_130x25.png"] forState:UIControlStateNormal];
[buttonBar addTarget:self action:@selector(doSomethingBar:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonBar];
[buttonBar release];
实例方法 doSomethingFoo
和 doSomethingBar
-(void)doSomethingFoo:(id) sender {
// code doing something with a NSString having value 'foo'
}
-(void)doSomethingBar:(id) sender {
// same code doing something with a NSString having value 'bar'
}
我想做的是创建一个方法(doSomething
),我可以在按下任一按钮时调用它,并向其传递我将在该方法中引用的字符串(值“foo”或“bar”)。我正在为语法而苦苦挣扎。
Hi I have multiple UIButtons (foo
and bar
) and when pressed each invokes a different instance method (doSomethingFoo
and doSomethingBar
). Here is the code I have working:
CGRect fooImageRect = CGRectMake(38.0f, 192.0f, 130.0f, 25.0f);
UIButton *buttonFoo = [[UIButton alloc] init];
buttonFoo.frame = fooImageRect;
[buttonFoo setImage:[UIImage imageNamed:@"button_foo_130x25.png"] forState:UIControlStateNormal];
[buttonFoo addTarget:self action:@selector(doSomethingFoo:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonFoo];
[buttonFoo release];
CGRect barImageRect = CGRectMake(172.0f, 192.0f, 130.0f, 25.0f);
UIButton *buttonBar = [[UIButton alloc] init];
buttonBar.frame = barImageRect;
[buttonBar setImage:[UIImage imageNamed:@"button_bar_130x25.png"] forState:UIControlStateNormal];
[buttonBar addTarget:self action:@selector(doSomethingBar:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonBar];
[buttonBar release];
And the instance methods doSomethingFoo
and doSomethingBar
-(void)doSomethingFoo:(id) sender {
// code doing something with a NSString having value 'foo'
}
-(void)doSomethingBar:(id) sender {
// same code doing something with a NSString having value 'bar'
}
What I am trying to do is to create a single method (doSomething
) that I can invoke when either button is pressed, and pass it the string (value 'foo' or 'bar') that I'll reference in the method. I'm struggling with the syntax.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用相同的方法来处理这两个按钮,但无法选择发送的参数。操作选择器必须是以下形式之一:
其中
sender
是发送消息的控件。因此,您可以通过检查sender
来判断是两个按钮中的哪一个发送了消息。直接进行检查的可能性是,当您使用一些众所周知的值(#define 或其他常量)创建 UIButton 时设置 UIButton 的 tag 属性,并在操作方法中检查该属性。You can use the same method to handle both buttons, but you cannot choose the arguments that get sent. The action selector must be one of the following forms:
Where
sender
is the control that sent the message. So you'll be able to tell which of the two buttons sent the message by checking thesender
. On possibility to make the check straight-forward would be to to set the tag property of the UIButton when you create it with some well known value (a #define or some other constant) and check that property in your action method.只需创建一个名为 ButtonTitle 的 NSString ivar 并编写如下方法:
simply make an NSString ivar called buttonTitle and write the method like so: