2012-11-12 30 views
0

我想通过继承UIView会看起来像下面这样创建一个自定义类:
My target还有一个自定义UIView子类

哦,文中说身体看法应该是80px高,其50像素。
但我似乎无法正确理解。我有类头准备,像这样:

#import <UIKit/UIKit.h> 

@interface MessageView : UIView 

@property (strong, nonatomic)UIImage *backgroundImage; 
@property (strong, nonatomic)UITextView *messageView; 
@property (strong, nonatomic)UILabel *titleLabel; 

- (id)initWithBackgroundImage:(NSString*)background; 

- (void)setTitle:(NSString*)titleMessage; 
- (void)setBody:(NSString*)bodyMessage; 

@end 

现在,我将创建通过调用initWithBackgroundImage这些观点,我将通过背景图片的文件名。该示例显示了这些图像中的一个。我如何实现init方法,以便init将像例子一样创建一个body,除了文本将是空的?

我的init方法的企图失败是这样的:

- (id)initWithBackgroundImage:(NSString*)background 
{ 
    self = [super init];//[super initWithFrame:CGRectMake(0, 0, 270, 100)]; 
    if (self) 
    { 
     backgroundImage = [[UIImage imageNamed:background] retain]; 
     messageView = [[UITextView alloc] initWithFrame:CGRectMake(10, 40, 250, 50)]; 
     titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 250, 20)]; 
    } 
    return self; 
} 

但是什么都没有发生。完成后,我将通过setter方法设置文本。我想通过setCenter(x, y)在最终视图中设置位置。我希望班级具有“固定”的宽度和高度。

谢谢!

回答

0

编辑:

- (id)initWithBackgroundImage:(NSString*)background 
    { 
     self = [super init];//[super initWithFrame:CGRectMake(0, 0, 270, 100)]; 
     if (self) 
     { 
      UIImageView *bg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:background]];  
      messageView = [[UITextView alloc] initWithFrame:CGRectMake(10, 40, 250, 50)]; 
      titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 250, 20)]; 
      [self addSubview:bg]; 
      [bg release]; 
      [self addSubview:messageView]; 
      [messageView release]; 
      [self addSubview:titleLabel]; 
      [titleLabel release]; 
     } 
     return self; 
} 
+0

通过使用'[[ALLOC消息查看] initWithBackgroundImage:@ “message_red”];'它崩溃与日志:'***终止应用程序由于未捕获的异常 'NSInvalidArgumentException',原因:' - [UIImage superview]:无法识别的选择发送到实例0x1dda7420'' – Majster

+0

ofcourse ..我编辑答案 –

+0

甜,它现在的工作。我只是忘记了“addSubview”。请问为什么我应该放弃我的房产?我将如何编辑它们? – Majster

相关问题