简体   繁体   中英

UITextView hasText not working?

I am assigning the contents of the clipboard to the UITextView text property. However, when I check the hasText property, the condition is always false.

NSString paste_text = [[NSString alloc] init];
self.paste_text = [UIPasteboard generalPasteboard].string; 

....

my_UITextView.text = self.paste_text;


//THIS CONDITION IS ALWAYS FALSE

if (my_UITextView hasText)
    { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:
            @"Text ready to copy"
            message:err_msg
            delegate:nil
            cancelButtonTitle:@"OK"
            otherButtonTitles:nil];

      [alert show];
    }

You need to send your UITextView the hasText message by using brackets:

if ([my_UITextView hasText])

UPDATE:

Do you know that your UTTextView has text? You might want to check it on the console:

my_UITextView.text = self.paste_text;

NSLog(@"my_UITextView.text = %@",my_UITextView.text); // check for text

//THIS CONDITION IS ALWAYS FALSE

if ([my_UITextView hasText])

First off, your paste_text isn't used as intended since right after you alloc, it will immediately be discarded, possibly released, maybe not. You could actually just do away with the [[NSString alloc] init]; .

Then add the following to test your code:

// delete:
//     NSString paste_text = [[NSString alloc] init];
NSLog([UIPasteboard generalPasteboard].string);
    self.paste_text = [UIPasteboard generalPasteboard].string; 

NSLog(self.paste_text);
    ....

    my_UITextView.text = self.paste_text;

NSLog(@"my_UITextView is %@, text contained: %@, my_UITextView , my_UITextView.text);

The first NSLog prints out the pasteboard string, the second the string once it is passed on to your paste_text , and the last will let you know if my_UITextView is non-nil, and what text it contains.

Also, if paste_text is a @property, what are its attributes? The text from [UIPasteboard generalPasteboard].string needs to be copied into it, otherwise when the pasteboard's string is changed, so is your paste_text .

How are you creating the my_UITextView property? If you did it in InterfaceBuilder, it's possible that you forgot to create an IBOutlet.

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