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 g开发者_如何学PythoneneralPasteboard].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.
精彩评论