Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Tutorials

Reply
 
LinkBack Thread Tools Display Modes
Old 02-09-2012, 12:20 PM   #1 (permalink)
Shmoopi LLC
 
Shmoopi's Avatar
 
Join Date: Jun 2009
Location: Virginia
Posts: 213
Shmoopi is on a distinguished road
Lightbulb Copy Text using UIPasteBoard

***This tutorial was written and re-posted with permission from: iPhoneSDK***

A useful feature to have in your iOS Apps is to give the user the ability to be able to copy content within your App. Whether it be text or images, you can copy most content. To do this, we need to use the UIPasteBoard feature of the iPhone SDK. In this tutorial we will have a UILabel, a UIButton and a UITextField. The label will hold the text to be copied, the button will fire the code to copy the text and then you will be able to paste the text into the textfield.

Step 1: Write the .h

First off in the ViewController.h we need in declare IBAction’s for our UILabel and UITextField, I named mine ‘label’ and ‘textField’ respectively. Next we need to create an IBAction for our button that will fire the code to copy the text. That’s it for the .h!

Code:
#import 

@interface ViewController : UIViewController {
    
    IBOutlet UILabel *label;
    IBOutlet UITextField *textField;
    
}

- (IBAction)copy:(id)sender;

@end
Step 2: Write the .m

Inside our IBAction in the .m, we want to set up the code that will copy the text from the UILabel into the UITextField. First off we create an NSString that holds the text of our label. On the next line, we set up the UIPasteBoard, I named mine ‘pb’, but you can name it anything you want. Next we set the text to be copied by the UIPasteBoard to be the NSString we set up earlier which is holding the text of our label.

That’s the coding part done!

Code:
#import "ViewController.h"

@implementation ViewController

- (IBAction)copy:(id)sender {
    
    NSString *copyString = label.text;
    UIPasteboard *pb = [UIPasteboard generalPasteboard];
    [pb setString:copyString];
    
}

@end
Step 3: Hook things up in Interface Builder

Now we need to set up the interface. To do that we’re going to use interface builder. Open up Interface Builder and drag a UILabel, UIButton and UITextField from the Library onto our view. Set the text of the label to be the text you want to be copied, set the title of the button to be ‘Copy Text’ and leave the textfield blank. Then click on ‘Files Owner’ and in the Inspectors sidebar select the ‘Connections’ tab. Then drag the received action ‘copy’, onto our button. Select Touch Up Inside. Then we need to hook up our label and textfield. So under ‘Outlets’ drag ‘label’ to the UILabel and ‘textField’ to the UITextField.

We’re done!



Step 5: Build and Run!

Now we can build the project and copy text using UIPasteBoard!



Download the sample project here!

That's it! Thank you very much for reading, and a special thanks to iPhoneSDK for writing the tutorial!

Links,
JAS Applications Tutorials
Original Tutorial
Shmoopi is offline   Reply With Quote
Reply

Bookmarks

Tags
copy, generalpasteboard, label, string, uipasteboard

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 468
14 members and 454 guests
alexeir, David-T, Dj_kades, foslock, iAppDeveloper, jeroenkeij, LunarMoon, Mijator, myach, pipposanta, QuantumDoja, robsmy, sacha1996, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,129
Posts: 402,928
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:25 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0