그래서 iOS와 초보자로서 잠시 동안이 문제를 해결해 왔습니다. 실종 된 기본 개념이거나 내가 아직 참조하지 않아도되는 속성입니다.self.view가 없을 때 UIView를 어떻게 찾을 수 있습니까?
시나리오 : View Controller는 UIScrollView를 만듭니다. UIView는 여러 UILabels (이벤트, 장소 및 시간 설명)의 컨테이너로 만들어집니다. 메서드는 반복적으로 호출되어 블록 내에서 이러한 UILabels를 만듭니다. 이러한 레이블을 하나씩 작성하면 뷰에 각 요소를 간단하게 추가 할 수 있습니다. 그러나 코드를 메서드로 옮기고 다시 사용하여 텍스트 크기, 들여 쓰기 등을 추상화하면 같은 부모 뷰를 참조 할 수 없습니다 (보기 컨트롤러가 아니기 때문에), 또는 viewWithTag (아무것도 반환하지 않음)를 사용하여 검색하여 부모를 찾으십시오.
이 간단한 수정입니까, 아니면 기본 구조에 결함이 있습니까? 많은 시간을 미리 감사드립니다!
헤더 :
//
// ScheduleColumn.h
//
#import <Foundation/Foundation.h>
@interface ScheduleColumn : UIView {
}
- (void)makeTextBlock:(int)parentViewID label:(NSString*)label textSize:(int)textSize indent:(int)indent y:(int)y width:(int)width height:(int)height;
@end
구현 :
//
// ScheduleColumn.m
//
#import "ScheduleColumn.h"
@implementation ScheduleColumn
// makeTextBlock: type, text, textSize, indent, build_y, width, height
// type: 0 = Title, 1 = Subtitle, 2 = Times
// text: Line content
// textSize: self-explanatory
// indent: indent from left side of parent
// build_y: # of units down from top of parent view to build
// width & height: self-explanatory
- (void)makeTextBlock:(int)parentViewID label:(NSString*)label textSize:(int)textSize indent:(int)indent y:(int)y width:(int)width height:(int)height {
double unixTime;
unixTime = [[NSDate date] timeIntervalSince1970];
NSLog(@"makeTextBlock called");
NSLog(@"parentViewID: %u", parentViewID);
NSLog(@"label: %@", label);
NSLog(@"textSize: %u", textSize);
NSLog(@"indent: %u", indent);
NSLog(@"y: %u", y);
NSLog(@"width: %u", width);
NSLog(@"height: %u", height);
NSLog(@"time: %u", unixTime);
UILabel *textView = [[UILabel alloc] initWithFrame:CGRectMake(indent, y, width, height)];
textView.backgroundColor = [UIColor clearColor];
textView.textColor = [UIColor whiteColor];
textView.lineBreakMode = UILineBreakModeWordWrap;
textView.numberOfLines = 0;
textView.tag = unixTime;
textView.font = [UIFont fontWithName:@"PetitaMedium" size: textSize];
textView.text = label;
CGSize constraintTextSize;
constraintTextSize.width = width;
constraintTextSize.height = MAXFLOAT;
CGSize theTextSize = [label sizeWithFont:[UIFont fontWithName:@"PetitaMedium" size: textSize] constrainedToSize:constraintTextSize lineBreakMode:UILineBreakModeWordWrap];
CGRect newTextFrame = textView.frame;
newTextFrame.size.height = theTextSize.height;
textView.frame = newTextFrame;
UIView *parentView = (UIView *)[self.view viewWithTag:parentViewID];
[parentView addSubview:textView];
[textView release];
NSLog(@"--- break ---");
}
.. 그리고 마지막으로, 뷰 컨트롤러에서 전화 :
int build_y;
int subtitle_indent;
build_y = 30;
subtitle_indent = 20;
UIView *blockView = [[UIView alloc] initWithFrame: CGRectMake (0, build_y, 185, 50)];
blockView.tag = 100;
[FireworksContent addSubview:blockView];
// Add top line
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, blockView.bounds.size.width, 0.5)];
lineView.backgroundColor = [UIColor whiteColor];
[blockView addSubview:lineView];
// Add Block Text
ScheduleColumn *blockText = [ScheduleColumn alloc];
[blockText makeTextBlock:blockView.tag label:@"Venue" textSize:18 indent:subtitle_indent y:build_y width:blockView.bounds.size.width height:20];
[blockText makeTextBlock:blockView.tag label:@"ShowTitle" textSize:12 indent:subtitle_indent y:build_y width:blockView.bounds.size.width height:20];
[blockText makeTextBlock:blockView.tag label:@"Showtime" textSize:36 indent:subtitle_indent y:build_y width:blockView.bounds.size.width height:20];
[lineView release];
[blockText release];
[blockView release];
...을 viewWithTag 라인이 있기 때문에 실패 " self "는 뷰가 없다 ... UIViewController로 클래스를 변경하면 실행 가능하지만 여전히 기쁨은 없다.
.... 내가 인터넷을 좋아하는 이유입니다. 너 신사 야. 스크립트가 아닌 객체를 생각해야합니다! BTW, "addSubview"다음에 콜론이 필요합니다. :) 시간 내 주셔서 대단히 감사합니다! – JSpread