views:

152

answers:

3

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];
    }
+3  A: 

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])
gerry3
That does not seem to be working either.
Kevin McFadden
"That does not seem to be working either". This could mean:1. my_UITextView causes an exception2. that code makes your app barf3. my_UITextView.text contains garbage4. my_UITextView.text is nil5. my_UITextView.text is a zero length string6. I can go on...In order for us to help you, you need to do a little bit more work and let us know what "not working" is. Do a little debugging, provide us with the results, and then we're better armed to help you. See below on what debugging you could do.
mahboudz
A: 

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

bpapa
A: 

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.

mahboudz