Reputation: 1012
I have created UITextView
programatically, textview is created as required but when i try to set value for textview using tag application crashes.
I am able to set other parameter like setUserInteractionEnabled
without issue.
following the code for it..
To create textview
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(0,0,100,200)];
textView.font = [UIFont systemFontOfSize:30];
//textView.font = [UIFont boldSystemFontOfSize:12];
textView.backgroundColor = [UIColor grayColor];
textView.tag=i;
[textView setTextAlignment:NSTextAlignmentCenter];
textView.text=[NSString stringWithFormat:@"%d",i];
textView.inputView= [LNNumberpad defaultLNNumberpad];
textView.editable = YES;
[textView setDelegate:self];
[self.view addSubview:textView];
Changeing value in textview using tag..
UITextView *txtviewfound = (UITextView *)[self.view viewWithTag:j];
[txtviewfound setText:@"some text"];
[txtviewfound setUserInteractionEnabled:FALSE];
When i comment settext like it works fine.
If i am missing anything please let me know.
Thanks in advance
Upvotes: 1
Views: 746
Reputation: 469
What's the crash log saying? An index out if bound maybe? Also just to be sure - don't set tags with really low numbers like 1 to 5, they could, rarely but still, already be taken by Apple.
Upvotes: 0
Reputation: 8460
Before type casting you need to check weather subview belongs to textView Or not,because by default every object having 0 as a tag value.if you have any other objects are in your view then it'l creates problem.
if([[self.view viewWithTag:i]isKindOfClass:[UITextView class]])
{
UITextView *txtviewfound = (UITextView *)[self.view viewWithTag:j];
[txtviewfound setText:@"some text"];
[txtviewfound setUserInteractionEnabled:NO];
}
Upvotes: 1