【发布时间】:2012-12-04 06:52:26
【问题描述】:
我有两个UITextViews,self.instructions 和 self.textView,它们应该根据用户的选择交替出现。
我像这样创建self.textView:
-(void)createSpaceToWrite
{
[self.instructions removeFromSuperview];
[self.bar removeFromSuperview];
[self createNavigationBar:@"Compose" selector:@"displayScreen" withDone:NO]; //This adds a UINavigationBar to the view.
if (!self.textView)
{
self.textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 60, 280, 150)];
}
self.textView.backgroundColor = [UIColor whiteColor];
self.textView.font = [UIFont fontWithName:@"Helvetica Neue" size:14];
self.textView.text = @"";
[self.view addSubview:self.textView];
self.textView.delegate = self;
}
然后我像这样创建self.instructions:
-(void)haikuInstructions
{
[self.textView removeFromSuperview];
[self.bar removeFromSuperview];
[self createNavigationBar:@"Compose" selector:@"displayScreen" withDone:NO];
if (!self.instructions)
{
self.instructions = [[UITextView alloc] initWithFrame:CGRectMake(10, 125, [[UIScreen mainScreen] bounds].size.width - 10, [[UIScreen mainScreen] bounds].size.height)];
}
self.instructions.backgroundColor=[UIColor clearColor];
self.instructions.text = @"Text of instructions";
self.instructions.editable=NO;
[self.view addSubview:self.instructions];
[self resignFirstResponder];
}
用户从背景图像上显示的self.instructions 开始。很好。
用户切换。说明文字消失,取而代之的是可编辑的self.textView,一个白框。很好。
用户切换回来。说明文本出现了——但白框仍然存在,即使我认为我已将其从超级视图中删除。不仅如此,它仍然是可编辑的,并且当用户去编辑它时仍然会弹出键盘!
我做错了什么?
编辑:好吧,我基本上废弃了所有代码并从头开始上课,试图让所有事情变得更干净,我不再遇到这个问题,所以它一定是其他一些影响它的方法。教训:随意编码是不好的!
【问题讨论】:
-
self.textView=nil;在[self.textView removeFromSuperview];之后尝试这一行 -
感谢您的想法,但它并没有改变任何东西。 :(
-
我测试了您的代码似乎可以正常工作,尽管我评论了
createNavigationBar:selector:withDone:并添加了一些按钮来调用这两种方法。您是否正确调整了 UIViewController 头文件中的,您是否将此属性声明为强,例如 @property (nonatomic,strong) UITextView *textView和@property (nonatomic,strong) UITextView *instructions -
这两个属性都被声明为强。我已经包含了
,但我不知道“适应”它是什么意思。你介意解释一下吗?
标签: objective-c ios uitextview