如何找到UIView...当没有自我视图时?



作为iOS新手,我一直在与这个问题作斗争——我相信这要么是我遗漏了一个基本概念,要么是我还没有遇到需要参考的属性。

场景:视图控制器创建一个UIScrollView。UIView被创建为几个uilabel的容器(描述一个事件,地点和时间)。方法被反复调用以在块中创建这些uilabel。一个接一个地创建这些标签很好-简单地将每个标签添加到视图中-但是当我将代码移动到方法并重用它时,抽象出诸如文本大小,缩进等内容,我不能引用相同的父视图(因为它不是视图控制器?),或使用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让它运行,但仍然没有乐趣。

返回新视图的类方法比返回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];

最新更新