2011-10-28 2 views
0

사전에 키 값으로 배열에 단어를 만들려고합니다. 그러나 UIImageView를 "복사"할 수 없기 때문에 원하는 방식으로 작동하지 않습니다.NSMutableDictionary에 UIImageViews가 포함되어 있습니다 ... NSMutableArray에 변경 가능한 사본을 추가 하시겠습니까? 대체?

사전에있는 문자를 배열에 추가 할 때 실제 개체 대신 사본이 제공되는 경우 완벽 할 것입니다. 동일한 문자를 여러 이미지로 만들고 사전에 추가하고 싶지 않습니다. 더 이상 "s"또는 "a"키로 부를 수 없기 때문에 같은 문자를 사용하려면 둘 이상의 배열이 필요합니다. 시간에.

어떻게해야합니까?

//How I create the letters 
     char s = 's'; 
     NSString *key = [NSString stringWithFormat:@"%c", s]; 
     alphabetS = [[UIImageView alloc] init]]; 
     [alphabetS setImage:[UIImage imageNamed:@"s.png"]]; 
     [allTilesDictionary setObject:alphabetS forKey:key]; 
     [alphabetS release]; 


//How I use the imageviews from the dictionary 
    NSMutableArray *wordOne = [[NSMutableArray alloc] initWithObjects:[allTilesDictionary objectForKey:@"s"],[allTilesDictionary objectForKey:@"h"],[allTilesDictionary objectForKey:@"o"],[allTilesDictionary objectForKey:@"p"], nil]; 

편집 : 내 솔루션. 그것은 완벽하게 작동합니다. 약간 수정

for (UIImageView *letters in wordOne) 
{ 

    newLetter = [[UIImageView alloc] init]; 
    newLetter.image = letters.image; 
    newLetter.userInteractionEnabled = YES; 

    //I can now either lay them out wherever I want on the view, or add them to a new array. 
} 
+0

왜 이미지보기가 아닌 이미지를 저장 하시겠습니까? 이미지를 공유 할 수 있습니다. –

+0

왜 사본이 필요합니까? 그것에 대해 생각해보십시오, 아마 당신은 복사를 피할 수 있습니다. 그렇지 않으면 UIImageView의 사용자 정의 하위 클래스에 NSCopying 프로토콜을 구현해야합니다. 그리 쉬운 일은 아닙니다. 그것에 대해 읽어보십시오 [여기] (http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Protocols/NSCopying_Protocol/Reference/Reference.html) –

+0

나는 완벽한 생각을 생각합니다. 해결책. 나는 좋은 밤의 잠을 취했고, 일어 났고, 소나기에서 그것을 생각했다. 내가 생각하는 방식대로 작동한다면 솔루션을 게시하고 사람들은 궁금해 할 것입니다. 내 솔루션은 다니엘이 말하는 것과 같은 것을 사용합니다. – Jason

답변

0

원래 코드 : 해당 코드의

//How I create the letters 
     char s = 's'; 
     NSString *key = [NSString stringWithFormat:@"%c", s]; 
     [allTilesDictionary setObject:[UIImage imageNamed:@"s.png"] forKey:key]; 


//How I use the imageviews from the dictionary 
    NSMutableArray *wordOne = [[NSMutableArray alloc] initWithObjects:[allTilesDictionary objectForKey:@"s"],[allTilesDictionary objectForKey:@"h"],[allTilesDictionary objectForKey:@"o"],[allTilesDictionary objectForKey:@"p"], nil]; 

귀하의 사용은 약간 수정되지 : 생성

for (UIImage *letters in wordOne) 
{ 

    newLetter = [[UIImageView alloc] init]; 
    newLetter.image = letters; 
    newLetter.userInteractionEnabled = YES; 

    //I can now either lay them out wherever I want on the view, or add them to a new array. 
} 

없음 불필요한 UIImageViews을.

0
for (UIImageView *letters in wordOne) 
{ 

    newLetter = [[UIImageView alloc] init]; 
    newLetter.image = letters.image; 
    newLetter.userInteractionEnabled = YES; 

    //I can now either lay them out wherever I want on the view, or add them to a new array. 
} 
+0

하지만 불필요하게 UIImageViews를 "글자"로 만들어야합니다. 그 대신에 이미지를 넣으십시오. –

+0

아. 팁 고마워. :) – Jason

관련 문제