我有一个名为 Layout 的类,在这个类中有一些函数,如 setTextView 等。然后在我的 FirstViewController 中,我需要在 View Controller 上设置该布局。
例如,我的 Layout.m 如下所示:
-(UITextView*) setTextView{
UITextView *textView1 =[[UITextView alloc]init];
textView1.frame=CGRectMake(0,50,282,210);
textView1.backgroundColor = [UIColor redColor];
[textView1 setReturnKeyType:UIReturnKeyDone];
return textView1;
}
在我的 FirstViewController.m 中我得到了类似的东西:
Layout *test;
UITextView *textView1 = [test setTextView];
[self.view addSubview:textView1];
[textView1 removeFromSuperview];
test = nil;
但这不起作用,我做错了什么?
最佳答案
嗯...该代码没有多大意义。您正在执行 [test setTextView];
,即向名为 test
的对象发送消息,并在下一行中将该对象定义为 Layout
.
然后,您将从其 super View 中删除该测试对象,但您从未将其添加到任何 View 中(无论如何都不在显示的代码中)
我将在这里进行大胆的猜测,但我认为这就是您想要做的:
Layout* test = [[Layout alloc] init]; //You should be calling here whatever initialization method this class has
UITextView *textView1 = [text setTextView];
[self.view addSubview:textView1];
test = nil;
我仍然不确定你想在这里完成什么。也许如果你尝试解释一下我可以更好地帮助你。
关于ios - 在类中设置和删除布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15063942/