Iphone 将URL传递到方法
嗨,我有个问题 我能够在showImage方法中成功地获得“imageName”变量中的UrL字符串存储 我需要在方法“segmentedControlIndexChanged”中执行相同的操作,但我无法获取变量“imageName” 我是否可以声明iAction方法的工作方式与showImage方法类似 我尝试了->“Iphone 将URL传递到方法,iphone,xcode,methods,Iphone,Xcode,Methods,嗨,我有个问题 我能够在showImage方法中成功地获得“imageName”变量中的UrL字符串存储 我需要在方法“segmentedControlIndexChanged”中执行相同的操作,但我无法获取变量“imageName” 我是否可以声明iAction方法的工作方式与showImage方法类似 我尝试了->“-(iAction)segmentedControlIndexChanged:(NSString*)imageName{}”但它无法工作,我无法获取变量“imageName” 编
-(iAction)segmentedControlIndexChanged:(NSString*)imageName{}
”但它无法工作,我无法获取变量“imageName”
编辑
我遇到了错误的访问错误,有人知道这是什么意思,或者有人能告诉我一些解决方法吗?没有,iAction在iOS中只能有以下三个签名之一:
- (void)action
- (void)action:(id)sender
- (void)action:(id)sender forEvent:(UIEvent *)event
编辑
您可以扩展UIButton以存储所需的值,如:
@interface MyButton : UIButton {
}
@property (nonatomic, retain) NSString *url;
@end
...
@implementation MyButton
@synthesize url;
@end
...
- (void)buttonTapped:(id)sender {
MyButton *button = (MyButton *)sender;
UIImage *image = [UIImage imageNamed:button.url];
...
}
编辑
您可以使用标记来区分不同的发件人,如
enum {
ButtonOneTag = 128,
ButtonTwoTag,
...
}
...
- (void)buttonTapped:(id)sender {
UIButton *button = (UIButton *)sender;
switch(button.tag) {
case ButtonOneTag:
NSLog(@"Button One Tapped");
break;
case ButtonTwoTag:
NSLog(@"Button Two Tapped");
break;
}
您也可以将发送方与IVAR进行比较,以获得如下按钮:
- (void)buttonTapped:(id)sender {
//assuming buttonOne and buttonTwo are IBOutlets on this class
if (sender == self.buttonOne) {
NSLog(@"Button One Tapped");
else if (sender == self.buttonTwo) {
NSLog(@"Button Two Tapped");
}
...
}
所以,如果我有18个按钮调用同一个方法,我需要18个案例?基本上,是的,但是你可以扩展UIButton来存储url,如果这可以简化事情的话。我在上面添加了一个示例。
- (void)buttonTapped:(id)sender {
//assuming buttonOne and buttonTwo are IBOutlets on this class
if (sender == self.buttonOne) {
NSLog(@"Button One Tapped");
else if (sender == self.buttonTwo) {
NSLog(@"Button Two Tapped");
}
...
}