当没有自我观点时,如何找到一个UIView?

时间:2011-04-20 13:40:36

标签: ios uiview uilabel hierarchy

所以我作为iOS的新手一直在争论这个 - 我敢肯定它是我缺少的一个基本概念,或者我还没有遇到的属性我需要引用。< / p>

场景:视图控制器创建UIScrollView。 UIView被创建为多个UILabel的容器(描述事件,地点和时间)。重复调用方法以在块内创建这些UILabel。逐个创建这些标签工作正常 - 只需将每个标签添加到视图中 - 但是当我将代码移动到方法并重用它,抽象诸如文本大小,缩进等内容时,我无法引用相同的父视图(因为它不是View Controller?),或使用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 ---");

}

..最后,来自View Controller的调用:

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让它运行,但仍然没有乐趣。

1 个答案:

答案 0 :(得分:1)

返回新视图而不是返回void的实例方法的类方法会更有意义。

+(UIView *)makeTextBlock:(int)parentViewID label:(NSString*)label textSize:(int)textSize indent:(int)indent y:(int)y width:(int)width height:(int)height

根据需要创建视图,并在方法结束时返回该视图。

然后,您可以创建其中一些视图,并根据需要在视图控制器中保存对它们的引用。

UIView *blockText1 = [ScheduleColumn makeTextBlock .....];
[self.view addSubview: blockText1];