TonyTakeshi
TonyTakeshi

Reputation: 5929

How to manually initialize custom UIView

This might be a simple question, but I found myself stuck. I tried to extend UIView and initalize it from another controller, but found out my custom UIView initialize method didn't called. How do I initialize my custom UIView?

// BookView.h
@interface BookView : UIView {
}

//BookView.m
@implementation BookView
- (void) initialize{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Alert" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    [alert show];
    [alert release];
}
@end

//BookViewController.m
-(void)viewDidLoad{
    BookView *bookView = [(BookView*)[BookView alloc] initWithTest:[[UIScreen mainScreen] bounds]];
}

Upvotes: 2

Views: 3096

Answers (3)

gotnull
gotnull

Reputation: 27214

//BookView.m
@implementation BookView
- (void) init { // changed from (void)initialize {}
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Alert" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    [alert show];
    [alert release];
}

..and then apply @Navaneethan init of your BookView.

Upvotes: 0

Navaneethan
Navaneethan

Reputation: 2225

try this,

BookView *bookView =[[BookView alloc] init];
bookView.frame=self.view.frame;
[self.view addSubView:bookView];
[bookView release];

Upvotes: 1

gotnull
gotnull

Reputation: 27214

NSArray *nibViews =  [[NSBundle mainBundle] loadNibNamed:@"BookView" owner:self options:nil];
self.yourBookView = [nibViews objectAtIndex:0];

Upvotes: 0

Related Questions