Linux UITextview viewWithTag - 当我知道我可以访问它时,更新文本不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3677163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UITextview viewWithTag - updating the text doesn't work while I know I have access to it
提问by chewy
Hey There, your kind help on a small matter.
你好,你在一件小事上的帮助。
I have a UITextview with a tag
我有一个带有标签的 UITextview
// Text view
UITextView *currentPage = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, 300.0, 194.0)];
currentPage.scrollEnabled = YES;
currentPage.textAlignment = UITextAlignmentLeft;
currentPage.tag = 100;
currentPage.editable = NO;
currentPage.text = @"All is sweet";
[self.view addSubview:currentPage];
[currentPage release];
Some time later, I want to change the above "currentPage" text and replace it's content with some new text.
一段时间后,我想更改上面的“currentPage”文本并将其内容替换为一些新文本。
-(void)loadNewPage{
// works fine, meaning I am "touching" the right object
[self.view viewWithTag:100].alpha = 0.5f;
// So why this one isn't legal syntax ?
//[self.view viewWithTag:100].text = @"A new updated text";
}
The viewWithTag works fine while changing the alpha, Thus I am left wondering about the right syntax to apply for the text update...
viewWithTag 在更改 alpha 时工作正常,因此我想知道应用文本更新的正确语法......
采纳答案by Vladimir
-viewWithTag:
method returns UIView object, so to set properties specific to UITextview you should cast its type explicitly:
-viewWithTag:
方法返回 UIView 对象,因此要设置特定于 UITextview 的属性,您应该显式转换其类型:
((UITextView*)[self.view viewWithTag:100]).text = @"A new updated text";