Я перефакторизую свой код и хочу переместить целую кучу UILabels в другой класс, чтобы немного поработать. Мне не хватает одной части головоломки, чтобы иметь возможность сделать это, хотя (или, может быть, я просто устал lol) Во всяком случае, это упрощенный код, показывающий мою проблему. Заранее спасибо всем, кто помогает :)Программно Отображение UILabel из другого класса
@interface MyClass : UIView {
UILabel *classLabel;
}
@property (assign) UILabel *classLabel;
@end
@implementation MyClass
@synthesize classLabel;
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
}
return self;
}
- (void)dealloc {[super dealloc];}
@end
@interface LabelTestViewController : UIViewController {
MyClass *myClassInstance;
UILabel *myLabel;
}
@property (assign) UILabel *myLabel;
@end
@implementation LabelTestViewController
@synthesize myLabel;
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// this shows a label on the screen as expected
myLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 50, 20)];
myLabel.text = @"Hello";
[self.view addSubview:myLabel];
[myLabel release];
// this doesn't show anything on the scree
myClassInstance = [MyClass new];
[myClassInstance drawRect:CGRectMake(10, 50, 50, 20)]; // I suspect I need to call a different method, just don't know which one. initWithFrame is what I used at the time of creation of the label in the previous working scenario. is there an equivalent?
myClassInstance.classLabel.text = @"Goodbye";
[self.view addSubview:myClassInstance.classLabel];
}
- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];}
- (void)viewDidUnload {}
- (void)dealloc {[super dealloc];}
@end
В пункте 2 я не делаю ничего, чтобы установить ярлык в MyClass, кроме объявления его существования. Я действительно только помещаю ярлык там, потому что в моем реальном коде это логичное место для него, и это делает мой основной код более менее раздутым (есть кучи экземпляров класса, связанные с определенными метками). Если я хочу добавить myClassInstance.classLabel в self.view, мне не нужно сначала добавлять представление MyClass в self.view? Я попробую ваше предложение переменной CGRect. Я в курсе пункт-тоже, я просто хватаюсь за соломинку в этот момент :) Спасибо за ответ :) – Timbo
OMG лол, как неловко все, что мне нужно было положить в myClassInstance.classLabel = [[UILabel alloc] initWithFrame: CGRectMake (0, 0, 40, 18)]; когда я уже объявил myClassInstance – Timbo