将 URL 传递到方法中

发布于 2024-10-14 10:50:02 字数 2742 浏览 1 评论 0原文

您好,我在这里有一个问题,

我可以在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绿萝 2024-10-21 10:50:02

不可以,在 iOS 中,IBAction 只能具有以下三个签名之一:

- (void)action
- (void)action:(id)sender
- (void)action:(id)sender forEvent:(UIEvent *)event

EDIT

您可以扩展 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];
    ...
}

EDIT

您可以使用标签来区分不同的发件人,例如

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;
}

您也可以将发件人与按钮的 ivars 进行比较,如下所示:

- (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");
    }
    ...
}

No, an IBAction can only have one of the following three signatures in iOS:

- (void)action
- (void)action:(id)sender
- (void)action:(id)sender forEvent:(UIEvent *)event

EDIT

You could extend UIButton to store the value you need, like:

@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];
    ...
}

EDIT

You can use tags to differentiate between different senders, like

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;
}

You can also just compare the sender to ivars for the buttons like so:

- (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");
    }
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文