0

I would like to create a custom class by subclassing the UIView that would look like the one below:
My target

Oh and the text says the body view should be 80px tall, its 50px.
But I cant seem to get it right. I have the class header prepared like so:

#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

Now I will create these views by calling initWithBackgroundImage where I will pass the background image file name. The example shows one of these images. How do I implement the init method so that init will create a body like on the example except the texts will be empty?

My futile attempt of the init method was this:

- (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;
}

But nothing happened. When done I will set the text via setter methods. And I would like to set the position on the final view via setCenter(x, y). I want the class to have "fixed" width and height.

Thanks!

Majster
  • 3,611
  • 5
  • 38
  • 60

1 Answers1

0

EDIT:

   - (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;
}
Evgeniy S
  • 1,464
  • 12
  • 32
  • By using `[[MessageView alloc] initWithBackgroundImage:@"message_red"];` it crashes with log: `*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImage superview]: unrecognized selector sent to instance 0x1dda7420'` – Majster Nov 12 '12 at 11:03
  • Sweet, Its working now. I was just forgetting "addSubview". May I ask why should I release my properties? How am I going to edit them? – Majster Nov 12 '12 at 11:15
  • 1
    @property (retain, nonatomic)UILabel *titleLabel; - here you add 1 link to property; titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 250, 20)]; - here you add second link. [titleLabel release]; - now you have 1 link. isn't? :) – Evgeniy S Nov 12 '12 at 11:25