How to set the UIPasteboard (to a NSString)?

Discussion in 'iPhone/iPad Programming' started by stoopkitty, Feb 1, 2011.

  1. stoopkitty, Feb 1, 2011
    Last edited: Feb 1, 2011

    macrumors member

    Joined:
    Jan 26, 2010
    #1
    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.
     
  2. macrumors 603

    Joined:
    Aug 9, 2009
    #2
  3. stoopkitty, Feb 1, 2011
    Last edited by a moderator: Feb 1, 2011

    thread starter macrumors member

    Joined:
    Jan 26, 2010
    #3
    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?
     
  4. macrumors 603

    Joined:
    Aug 9, 2009
    #4
    Read the Memory Management Guide at the URL already provided. It tells you the rules of ownership.
     

Share This Page