当没有 self.view 时,如何找到 UIView?
作为 iOS 新手,我已经和这个问题斗争了一段时间了——我确信这要么是我缺少的一个基本概念,要么是我还没有遇到过需要参考的属性。
场景:视图控制器创建一个 UIScrollView。 UIView 被创建为多个 UILabel(描述事件、地点和时间)的容器。重复调用方法以在块内创建这些 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 可以让它运行,但仍然没有乐趣。
So I've been fighting this one for a while as novice with iOS - I'm sure it's either a basic concept I'm missing, or a property I haven't run across yet that I need to reference.
Scenario: View controller creates a UIScrollView. UIView is created as a container for several UILabels (describing an event, venue and time). Method is called repeatedly to create these UILabels within the block. Creating these labels one by one works fine - simply adding each to the view - but when I move the code to a method and reuse it, abstracting such things as text size, indent, etc, I can't refer to the same parent view (because it's not a View Controller?), or search using viewWithTag (returns nothing) to find the parent.
Is this a simple fix, or is my basic structure flawed? Many thanks in advance for your time!
Header:
//
// 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
Implementation:
//
// 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 ---");
}
.. and finally, the calls from the 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];
... the viewWithTag line fails because "self" doesn't have a view... changing the class to a UIViewController lets it run, but still no joy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
返回新视图的类方法比返回 void 的实例方法更有意义。
根据需要创建视图,并在方法结束时返回该视图。
然后,您可以创建其中几个视图,并根据需要在视图控制器中保存对它们的引用。
A class method that returns a new view rather than an instance method that returns void would make more sense.
Create the view as you want, and return that view at the end of the method.
Then you can create several of these views and hold a reference to them in your view controller if you want.