简体   繁体   中英

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

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

}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM