将URL传递给方法

时间:2011-01-26 01:53:12

标签: iphone xcode methods

嗨,我有一个问题,

我可以在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; 


      }

}

修改

我遇到Bad Access错误,有人知道这是什么意思,或者有人能告诉我一些解决方法吗?

1 个答案:

答案 0 :(得分:1)

不,IBAction在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;
}

您还可以将发件人与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");
    }
    ...
}