2012-12-04 2 views
0

UIView 내용을 어떻게 복제 할 수 있는지 알 수 없습니다. 또는 심지어 가능합니까?UIView 복제

UIView은 .xib 파일 내에 레이블과 버튼이 있습니다. 이제이보기를 다른 레이블 텍스트로만 n 번 복사하고 싶습니다.

이렇게하면 되겠지만, 그런 식으로는 마지막으로 표시된 개체 만 가져옵니다. _view1IBOutlet이며 _view1Label과 같습니다.

NSMutableArray *Info = [[NSMutableArray alloc]initWithCapacity:number.intValue]; 
    for(int i = 0; i != number.intValue; i++) 
    { 
     NSString *number = [NSString stringWithFormat:@"Zona%i ",[[NSUserDefaults standardUserDefaults] stringForKey:@"ObjectNumber"].intValue]; 
     NSString *zona = [NSString stringWithFormat:@"%@%i",number, i+1]; 
     NSString *parinkta = [[NSUserDefaults standardUserDefaults] stringForKey:zona]; 
     _view1Label.text = parinkta; 
     _view1.hidden = false; 
     CGRect newrect = CGRectMake(_view1.frame.origin.x, _view1.frame.origin.y + (80 * i), _view1.frame.size.width, _view1.frame.size.height); 
     _view1.frame = newrect; 
     [Info addObject:_view1]; 
    } 
    for(int i = 0; i != number.intValue; i++) 
    { 
     UIView *add = [Info objectAtIndex:i]; 
     [self.view addSubview:add]; 
    } 

난 당신이 내가 뭘하려고 오전 아이디어를 얻을 것 같아요, 아마도이 일을 내 생각은 totaly 잘못, 그래서 그 누구도 날이의 경로에 얻을 수 있도록 수 있을까?

답변

1

로드하는 각 복사본의 레이블 내용을 조정하여 펜촉에서 여러 번보기를로드하십시오. 이것은 훨씬 더 쉽고, 짧으며, 오류를 일으키는 경향이 적습니다. UIView 내용을 메모리에 복사하려고 시도하는 것보다 어렵습니다.

다음은 펜촉 내용을 액세스 할 수 UINib를 사용하는 예이다 :

UINib *nib = [UINib nibWithNibName:@"nibName" bundle:nil]; 
NSArray *nibContents = [nib instantiateWithOwner:nil options:nil]; 

// Now nibContents contains the top level items from the nib. 
// So a solitary top level UIView will be accessible 
// as [nibContents objectAtIndex:0] 

UIView *view = (UIView *)[nibContents objectAtIndex:0]; 

// assumes you've set the tag of your label to '1' in interface builder 
UILabel *label = (UILabel *)[view viewWithTag:1]; 
label.text = @"My new text"; 

그래서 당신이 원하는 각 펜촉 예를 들어 위의 코드를 반복합니다. 당신이 다음 n 뷰를 사용하면 뷰를 루프에 대한 최초의 내부 n 시간을 만들 필요가 보여하고자하는 경우 코드를 통해 첫 번째 뷰를 생성하는 경우

+1

사용하십시오'UINib' :

같은 루프에 대한 귀하의 변경

. –

+0

자세한 방법을 알려주세요. 지식 부족으로 인해 유감입니다. 간단한 예를 사용하면 훨씬 더 명확 해집니다. – Datenshi

+0

작은 예제를 추가하여 답변을 업데이트했습니다. 더 많은 것을 알고 싶다면 API 문서를 읽으십시오! – occulus

0

을 단일 뷰를 배치 할 수 없습니다 다음 방법을 사용할 수 있습니다. 효율적으로이 작업을 수행 할

for(int i = 0; i != number.intValue; i++) 
    { 
     NSString *number = [NSString stringWithFormat:@"Zona%i ",[[NSUserDefaults standardUserDefaults] stringForKey:@"ObjectNumber"].intValue]; 
     NSString *zona = [NSString stringWithFormat:@"%@%i",number, i+1]; 
     NSString *parinkta = [[NSUserDefaults standardUserDefaults] stringForKey:zona]; 
     UIView *tempView = [[UIView alloc] init]; 
     UILabel *tempLabel = [[UILabel alloc] init]; 
     tempLabel.frame = _view1Label.frame; 
     tempLabel.text = _view1Label.text; 
     [tempView addSubview: tempLabel]; 
     tempView.frame = _view1.frame; 
     _view1Label.text = parinkta; 
     _view1.hidden = false; 
     CGRect newrect = CGRectMake(_view1.frame.origin.x, _view1.frame.origin.y + (80 * i), _view1.frame.size.width, _view1.frame.size.height); 
     _view1.frame = newrect; 
     [Info addObject:tempView]; 
    } 
+0

이것은 게시 한 코드의 문제를 해결할 수 있지만 잘못된 접근 방식을 전적으로 권장합니다. – occulus