PDA

View Full Version : How to set the UIPasteboard (to a NSString)?




stoopkitty
Feb 1, 2011, 06:33 PM
I have a UITextView called textview and why wont this IBAction work?

- (IBAction) copyToPasteboard{
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];

pasteboard.string = textview.text; // I first tried this, and when it didn't work, I tried:

[pasteboard setValue:textview.text forPasteboardType:@"public.utf8-plain-text"]; //this, and this didn't work either.

[pasteboard release];
}

What happens is it compiles properly and everything and everything works great until I click the UIButton linked to this IBAction, then the application crashes. I have tried using breakpoints and debugging, I'm not sure what the problem is but I think I have made a syntactical error.

EDIT: From the debugging and breakpoints, the problem is with the two lines of code that have comments appended to them. BTW, I tried those lines of code each individually, and neither of them worked.

EDIT 2: Also, I am able to successfully by using the code UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];

if ([pasteboard containsPasteboardTypes: [NSArray arrayWithObject:@"public.utf8-plain-text"]]){
//its a string
textview.text = [NSString stringWithFormat:@"%@",pasteboard.string];
}

[pasteboard release]; read from the pasteboard, just not write to it.



chown33
Feb 1, 2011, 07:23 PM
[pasteboard release];


Do you own the pasteboard object or not?

If not, should you be releasing something you don't own?
http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html

stoopkitty
Feb 1, 2011, 08:19 PM
Do you own the pasteboard object or not

Yes, I think I own the pasteboard object...didn't i create it when i did UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];?

EDIT:

so when i use

UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];

i'm not allocating any memory, so its not my job to release the variable?
is the only time when you need to release a object when you use "alloc", or are there ways to create objects that i would be responsible for releasing without using alloc?

chown33
Feb 1, 2011, 09:04 PM
is the only time when you need to release a object when you use "alloc", or are there ways to create objects that i would be responsible for releasing without using alloc?

Read the Memory Management Guide at the URL already provided. It tells you the rules of ownership.