Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

stoopkitty

macrumors member
Original poster
Jan 26, 2010
62
0
I have a UITextView called textview and why wont this IBAction work?

Code:
- (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
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.
 
Last edited:
Do you own the pasteboard object or not

Yes, I think I own the pasteboard object...didn't i create it when i did
Code:
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?
 
Last edited by a moderator:
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.