如何手动初始化自定义UIView

时间:2011-05-30 05:50:42

标签: iphone cocoa-touch

这可能是一个简单的问题,但我发现自己陷入困境。我试图扩展UIView并从另一个控制器初始化它,但发现我的自定义UIView初始化方法没有调用。如何初始化我的自定义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]];
}

3 个答案:

答案 0 :(得分:1)

试试这个,

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

答案 1 :(得分:0)

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

答案 2 :(得分: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];
}

..然后申请BookView的@Navaneethan初始化。