Search code examples
iosiphoneuitextviewsettextuitextviewdelegate

settext to uitextview app crash - ios sdk


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


Solution

  • 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];
    
    }