2011-04-20 31 views
1

因此,我一直在用iOS作爲新手來戰鬥這個遊戲 - 我確定它不是我缺少的一個基本概念,或者是我還沒有碰到的一個屬性,我需要引用它。當沒有self.view時,如何找到UIView?

場景:視圖控制器創建一個UIScrollView。 UIView作爲多個UILabels(描述事件,地點和時間)的容器而創建。重複調用方法在塊內創建這些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線失敗,因爲「自我「沒有觀點...將類更改爲UIViewController讓它運行,但仍然沒有喜悅。

回答

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]; 
+0

.... aaaand這就是爲什麼我喜歡上網。先生,你是個紳士。我需要在對象中思考,而不是腳本!順便說一句,在那之後需要冒號「addSubview」。 :) 非常感謝你花時間陪伴! – JSpread 2011-04-20 15:20:01

相關問題