将 URL 传递到方法中
您好,我在这里有一个问题,
我可以在 showImage 方法中成功获取“imageName”变量中存储的 UrL 字符串。
我需要在这个方法“segmentedControlIndexChanged”中执行相同的操作,但我无法获取变量“imageName”。
无论如何,我可以声明 IBAction 方法与 showImage 方法类似吗?
我尝试过 ->" -(IBAction) segmentedControlIndexChanged:(NSString *)imageName{}
" 但它无法工作,我无法获取变量“imageName”。
- (void) showImage:(NSString *)imageName
{
NSData *imageData;
//NSString * string1 = [NSString stringWithFormat:imageName];
imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imageName]];
//NSLog(@"%@",imageName);
image = [[UIImage alloc] initWithData:imageData];
self.imageView.image = image;
[imageView setFrame:CGRectMake(5, 10, image.size.width, image.size.height)];
[scrollview setScrollEnabled:YES];
if(image.size.height+20 >= 224)
{
[scrollview setContentSize:CGSizeMake(289, image.size.height + 20)];
}
else {
[scrollview setContentSize:CGSizeMake(289, 224)];
}
}
////////
-(IBAction) segmentedControlIndexChanged{
switch (self.segmentedControl.selectedSegmentIndex) {
case 0:
image = [UIImage imageNamed:image1name];
self.imageView.image = image;
[imageView setFrame:CGRectMake(5, 10, image.size.width, image.size.height)];
[scrollview setScrollEnabled:YES];
if(image.size.height+20 >= 224)
{
[scrollview setContentSize:CGSizeMake(289, image.size.height + 20)];
}
else {
[scrollview setContentSize:CGSizeMake(289, 224)];
}
break;
case 1:
image = [UIImage imageNamed:image2name];
self.imageView.image = image;
[imageView setFrame:CGRectMake(5, 10, image.size.width, image.size.height)];
[scrollview setScrollEnabled:YES];
[scrollview setContentSize:CGSizeMake(289, image.size.height+20)];
break;
case 2:
image = [UIImage imageNamed:image3name];
self.imageView.image = image;
[imageView setFrame:CGRectMake(5, 10, image.size.width, image.size.height+1)];
[scrollview setScrollEnabled:YES];
[scrollview setContentSize:CGSizeMake(289, image.size.height+20)];
break;
default:
break;
}
}
编辑
我遇到了错误访问错误,有人知道这是什么意思,或者有人可以告诉我一些解决方法吗?
Hi i have a question here,
i'm able to get the UrL string store in "imageName" variable successfully in showImage method.
I need to do the same in this method "segmentedControlIndexChanged" but i cant get the variable "imageName".
Is there anyway i can declare the IBAction method to work similarly as the showImage method?
I Tried ->" -(IBAction) segmentedControlIndexChanged:(NSString *)imageName{}
" but it cant work, i'm unable to get the variable "imageName".
- (void) showImage:(NSString *)imageName
{
NSData *imageData;
//NSString * string1 = [NSString stringWithFormat:imageName];
imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imageName]];
//NSLog(@"%@",imageName);
image = [[UIImage alloc] initWithData:imageData];
self.imageView.image = image;
[imageView setFrame:CGRectMake(5, 10, image.size.width, image.size.height)];
[scrollview setScrollEnabled:YES];
if(image.size.height+20 >= 224)
{
[scrollview setContentSize:CGSizeMake(289, image.size.height + 20)];
}
else {
[scrollview setContentSize:CGSizeMake(289, 224)];
}
}
////////
-(IBAction) segmentedControlIndexChanged{
switch (self.segmentedControl.selectedSegmentIndex) {
case 0:
image = [UIImage imageNamed:image1name];
self.imageView.image = image;
[imageView setFrame:CGRectMake(5, 10, image.size.width, image.size.height)];
[scrollview setScrollEnabled:YES];
if(image.size.height+20 >= 224)
{
[scrollview setContentSize:CGSizeMake(289, image.size.height + 20)];
}
else {
[scrollview setContentSize:CGSizeMake(289, 224)];
}
break;
case 1:
image = [UIImage imageNamed:image2name];
self.imageView.image = image;
[imageView setFrame:CGRectMake(5, 10, image.size.width, image.size.height)];
[scrollview setScrollEnabled:YES];
[scrollview setContentSize:CGSizeMake(289, image.size.height+20)];
break;
case 2:
image = [UIImage imageNamed:image3name];
self.imageView.image = image;
[imageView setFrame:CGRectMake(5, 10, image.size.width, image.size.height+1)];
[scrollview setScrollEnabled:YES];
[scrollview setContentSize:CGSizeMake(289, image.size.height+20)];
break;
default:
break;
}
}
EDIT
I have encountered Bad Access error, anyone knows what does it mean or can anyone show me some ways to solve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不可以,在 iOS 中,IBAction 只能具有以下三个签名之一:
EDIT
您可以扩展 UIButton 来存储您需要的值,例如:
EDIT
您可以使用标签来区分不同的发件人,例如
您也可以将发件人与按钮的 ivars 进行比较,如下所示:
No, an IBAction can only have one of the following three signatures in iOS:
EDIT
You could extend UIButton to store the value you need, like:
EDIT
You can use tags to differentiate between different senders, like
You can also just compare the sender to ivars for the buttons like so: