2012-10-17 44 views
0

我將一串圖像加載到UIScrollView中,然後調整它們大小並在每個圖標上放置一個按鈕,因此當我點擊小圖像上的按鈕時,它會加載滿在一個新的視圖大小的圖像。將自定義參數傳遞給按鈕動作方法

我很努力地找到一種體面的方式來將圖像的URL作爲參數傳遞給點擊處理程序作爲單擊按鈕時的參數,並且從我所讀取的內容中傳遞參數到回調函數是不可能的。

下面是代碼我迄今,

for(Card *card in crd){ 

    pocketImage = [self imageWithImage:[UIImage imageNamed:@"pocket.png"] scaledToSize:CGSizeMake(245/2,80/2)]; 
    pocketImageView = [[UIImageView alloc] initWithImage:pocketImage]; 

    cardImage = [self maskImage:[UIImage imageNamed:@"animal.jpg"]]; 
    cardImageView = [[UIImageView alloc] initWithImage:cardImage]; 

    y = (80/2) * inc; 

    if (inc % 2){ 
     x = 125; 
    } else { 
     x = 10; 
     y += (80/2); 
    } 

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [btn addTarget:self action:@selector(testMe) forControlEvents:UIControlEventTouchUpInside]; 
    [btn setFrame:CGRectMake(x,y-40,pocketImageView.frame.size.width,pocketImageView.frame.size.height*2)]; 

    [pocketImageView setFrame:CGRectMake(x,y,245/2,80/2)]; 
    [cardImageView setFrame:CGRectMake(x,y-40,245/2,80/2)]; 

    //add the pocketImageView to the container 
    [imageContainer addSubview:cardImageView]; 
    [imageContainer addSubview:pocketImageView]; 
    [imageContainer addSubview:btn]; 

    inc++; 
} 

回調函數,

-(void)testMe { 
    NSLog(@"works"); 
} 

所以基本上是剛剛經歷的所有圖像的循環,再追加他們都到UIScrollView ,它工作正常,當我嘗試將自定義參數傳遞給testMe方法時,問題就出現了,但據我所知,這是不可能的,所以我如何能夠點擊按鈕然後顯示相應的完整大小形象在一個新的訴IEW?

回答

0

您可以使用iVar /(私有)@property來實現此目的。

只需設置你想擁有可用這樣的數據:

myTransferVariable = // something you need in you testMe method 

,並使用它像這樣

-(void)testMe { 
    NSLog(@"%@", myTransferVariable); 
} 
0

你應該能夠定義你的行動- (void)testMe:(id)sender {並將其指定爲@selector(testMe:) 。如果您然後爲每個按鈕分配一個標記(例如)到一個URL字符串數組中的偏移量,則該方法應該能夠使用((UIButton *)sender).tag找到與所選按鈕匹配的URL。

相關問題