2011-01-26 26 views
0

嗨,我這裏有一個問題,傳遞網址方法

我能夠獲得「imageName」變量中的URL字符串存儲成功在showImage方法。

我需要做同樣的方法「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; 


     } 

} 

編輯

我所遇到的錯誤訪問錯誤,任何人都知道這是什麼意思或任何人可以告訴我一些方法來解決呢?

回答

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

你也可以只發送比較高德的按鈕,像這樣:

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

所以,如果我有18個按鈕調用同樣的方法,我需要18例? – robobooga 2011-01-26 03:01:27