如何分配对象类?

时间:2011-02-10 13:12:41

标签: iphone xcode

我正在尝试使用一些按钮分配一个对象类,但无法在父类视图上显示数据

这里我正在尝试

对象类在下面......

#import "TabView.h"


@implementation TabView


- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
        myLabel.text = @"Stupid";
        [myLabel release];

    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)dealloc {
    [super dealloc];
}


@end

父类

#import "TabbarviewViewController.h"
#import "TabView.h"
@implementation TabbarviewViewController



/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view removeFromSuperview];
    TabView *mytab=[[TabView alloc]init ];
    NSLog(@"%@",mytab);


}



/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end

2 个答案:

答案 0 :(得分:1)

您必须在此行initWithFrame而不是TabView *mytab=[[TabView alloc]init ];上致电init

答案 1 :(得分:0)

你忘了在-initWithFrame中执行[self addSubview:myLabel]:在TabView类中