Ios 未显示自定义视图
我正在为我的项目使用Ios 未显示自定义视图,ios,objective-c,Ios,Objective C,我正在为我的项目使用MVC模型技术。如果我在viewdiload中直接在viewController文件中为custom view编写代码,则会出现view。但若我正在为视图创建另一个类,那个么它并没有显示标签 代码: -(UIView*)homeView:(UIView*)Hview { toplabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 18, Hview.frame.size.width,30)]; toplabel
MVC
模型技术。如果我在viewdiload
中直接在viewController
文件中为custom view
编写代码,则会出现view
。但若我正在为视图创建另一个类,那个么它并没有显示标签
代码:
-(UIView*)homeView:(UIView*)Hview
{
toplabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 18, Hview.frame.size.width,30)];
toplabel.text=@"NexgHomes.com";
toplabel.textColor=[UIColor whiteColor];
toplabel.textAlignment=NSTextAlignmentCenter;
toplabel.font=[UIFont fontWithName:@"Helvetica"size:18];
toplabel.backgroundColor=[UIColor blueColor];
[Hview addSubview:toplabel];
return Hview;
}
ViewController.m:
- (void)viewDidLoad{
[homepagVar homeView:self.view]; /* homepagVar is the variable of homePage class and homeView is method of homePage class */
[super viewDidLoad];
}
主页类:
-(UIView*)homeView:(UIView*)Hview
{
toplabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 18, Hview.frame.size.width,30)];
toplabel.text=@"NexgHomes.com";
toplabel.textColor=[UIColor whiteColor];
toplabel.textAlignment=NSTextAlignmentCenter;
toplabel.font=[UIFont fontWithName:@"Helvetica"size:18];
toplabel.backgroundColor=[UIColor blueColor];
[Hview addSubview:toplabel];
return Hview;
}
它不会以这种方式显示标签。为什么?请尝试下面的代码
- (void)viewDidLoad{
[super viewDidLoad];
[homepagVar homeView:self.view];
[self.view addSubview:homepagVar];
}
您必须在viewDidLoad中添加子视图,因为不同的类可能有不同的self.view实例。我首先猜测是标签被剪裁,因为它不适合矩形。嗯,在homeView方法中,您应该用Hview替换self.view。您不应该将子视图添加到
Hview
,而不是self.view吗?或者设置self.view=Hview(顺便说一句,按照惯例,您的变量应该命名为Hview
)。另外,您在哪里设置homepagVar
?我已经编辑过了吗?请再次查看我的代码@paulw11您是否已将主页类的实例分配给homePagVar
?也就是说,它不是零?homepagVar
是HomePage
类的变量,而不是UIView
类的变量。无论如何,谢谢你@lalitI没有为homePageVar分配内存,这就是为什么它不会出现的原因。分配之后,它出现了。我犯了一个愚蠢的错误@拉里特