2012-11-08 20 views
0

在這裏我的代碼顯示4個按鈕標題哪一個圖像視圖,我想顯示點擊提醒,如果點擊按鈕標題等於UIImageview圖像。這是我的代碼,如何檢查被點擊的按鈕標題等於圖像名稱在uiimageview

imageary=[[NSMutableArray alloc]initWithObjects:@"dear.jpg",@"donkey.jpg",@"elephant.jpg",@"fox.jpg",@"giraffe.jpg",@"goat.jpg",@"buffallo.jpg",@"bull.jpg",@"cow.jpg",@"crocodile.jpg", nil]; // declare array of names not images 
- (NSString *) convertToDisplayName:(NSString *)actual 
{ 
    return [actual stringByDeletingPathExtension]; //return image name without extension 
} 

按鈕方法

-(IBAction)methodset:(id)sender 
{ 

int i=0; 
for(UIView *view in self.view.subviews) 
{ 
    if([view isKindOfClass:[UIButton class]]) 
    { 
    mybutton = (UIButton *)view; 
    if(mybutton.tag == 1||mybutton.tag == 2||mybutton.tag == 3||mybutton.tag == 4) 
    { 
    i = rand() % [imageary count]; // set random image 
    animage.image=[UIImage imageNamed:[imageary objectAtIndex:i]]; 
    name=[self convertToDisplayName:[imageary objectAtIndex:i]]; 
    [mybutton setTitle:name forState:UIControlStateNormal]; 
    NSLog(@"current image name :%@",name); 
    i++; 

    } 
    } 
} 
} 

查閱即時面臨的問題與

  1. 蘭特(),而洗牌在該按鈕的一些值 重複。我怎樣才能在不重複的情況下保持數值,並在 UIButton中顯示。
  2. 我應該在哪裏比較圖片的名稱並點擊按鈕標題 是否等於或不是。請幫我解決這個問題。請做 需要。

回答

0

使用while循環蘭特位

while (i != mybutton.tag){ 
    i = rand() % [imageary count]; 
} 

剛過的圖像名稱和標題UIButton的比較,如果(mybutton.tag ......等等等等,因爲你希望它在for循環權?也縮進代碼:第使得它更易於閱讀

0
if ([btn.titleLabel.text isEqualToString:@"imageName"]) 

根據你的代碼

if ([btn.titleLabel.text isEqualToString: name]) 
0

你可以簡單地通過改變功能就像做:

-(IBAction)methodset:(id)sender 
{ 
    UIButton *mybutton = (UIButton *)sender; 
    if(mybutton.tag == 1||mybutton.tag == 2||mybutton.tag == 3||mybutton.tag == 4) 
    { 
    for(int loop = 0; loop<4;loop++) 
    { 
     animage.image=[UIImage imageNamed:[imageary objectAtIndex:i]]; 
     name=[self convertToDisplayName:[imageary objectAtIndex:i]]; 
     [mybutton setTitle:name forState:UIControlStateNormal]; 
     NSLog(@"current image name :%@",name); 
     if ([mybutton.titleLabel.text isEqualToString:[imageary objectAtIndex:i]]) 
     { 
      //titles equal 
     } 

     UIView *newView = [self.view viewWithTag:i]; 
     if([newView isKindOfClass:[UIButton class]]) 
     { 
      UIButton *newButton = (UIButton *)newView; 
      [newbutton setTitle:name forState:UIControlStateNormal]; 
     } 
    } 
    } 
} 
+0

只有點擊的按鈕標題只顯示,並且具有所有按鈕btn.title intilized相同的值 – Fazil

+0

WER,或者說,它mybutton.title吧? – Fazil

+0

抱歉,錯字,編輯 –