2

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]];
}
0

3 Answers 3

1

try this,

BookView *bookView =[[BookView alloc] init];
bookView.frame=self.view.frame;
[self.view addSubView:bookView];
[bookView release];
Sign up to request clarification or add additional context in comments.

2 Comments

If you don't release bookView, this will cause a memory leak.
change your - (void) initialize to - (void) init then it will work
0
NSArray *nibViews =  [[NSBundle mainBundle] loadNibNamed:@"BookView" owner:self options:nil];
self.yourBookView = [nibViews objectAtIndex:0];

1 Comment

Thank you for reply,but I did not use any nib file.
0
//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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.