Как се намира UIView, когато няма self.view?

Така че се боря с това известно време като начинаещ с iOS - сигурен съм, че това е или основна концепция, която ми липсва, или свойство, на което все още не съм се сблъсквал, което трябва да направя.

Сценарий: Контролерът на изглед създава UIScrollView. UIView е създаден като контейнер за няколко UILabels (описващи събитие, място и час). Методът се извиква многократно, за да създаде тези UILabels в рамките на блока. Създаването на тези етикети един по един работи добре - просто добавяне на всеки към изгледа - но когато преместя кода към метод и го използвам повторно, абстрахирайки неща като размер на текста, отстъп и т.н., не мога да се позова на същия родителски изглед (защото не е 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 му позволява да работи, но все още не е радост.


person JSpread    schedule 20.04.2011    source източник


Отговори (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];
person Michael Behan    schedule 20.04.2011
comment
....ааа и затова обичам интернет. Вие, сър, сте джентълмен. Трябва да мисля в обекти, а не в скриптове! Между другото, трябва двоеточие след addSubview. :) Благодаря много за отделеното време! - person JSpread; 20.04.2011