IOSDevelop applicationsClipboardThis document describes the functional tutorials.IOS, You can useClipboardImplement data sharing among and between applications. For example, you can copy a url from iPhone QQ and paste it into the safari browser to view the link content.
1. In iOS, the following three controls have the copy-and-paste function:
1. UITextView
2. UITextField
3. UIWebView
Ii. UIKit framework provides several classes and protocols for us to implement the clipboard function in our own applications.
1. UIPasteboard: You can write data to or read data.
2. UIMenuController: displays a shortcut menu for copying, cutting, and pasting selected items.
3. can1_maction: withSender in UIResponder: used to control which commands are displayed in the shortcut menu.
4. When you click a command on the shortcut menu, UIResponderStandardEditActions will be called.
3. The following items can be placed in the clipboard.
1. UIPasteboardTypeListString-string array, which contains the kUTTypeUTF8PlainText
2. UIPasteboardTypeListURL-URL array, including
3. UIPasteboardTypeListImage-Image array, including the kUTTypePNG and kUTTypeJPEG
4. UIPasteboardTypeListColor-color array
4. There are two types of clipboard:
System Level: Use UIPasteboardNameGeneral and UIPasteboardNameFind to create a system level clipboard. data will not be lost when the application is closed or uninstalled.
Application-level: by setting, data can be stored in the clipboard after the application is closed, but the data will be lost after the application is uninstalled. We can use pasteboardWithName: create: To create an instance.
After learning about this, we will use a series of examples to illustrate how to use the clipboard in an application.
Example:
1. Copy the clipboard text.
In the following example, a shortcut menu is displayed on tableview, where only the copy button is displayed. copy the data on tableview and paste it to the title.
Define a cell class CopyTableViewCell. The shortcut menu is displayed on this class to implement the copy function.
- @interface CopyTableViewCell : UITableViewCell {
- id delegate;}@property (nonatomic, retain) id delegate;
- @end
Implement CopyTableViewCell:
- #import "CopyTableViewCell.h"@implementation CopyTableViewCell@synthesize delegate;
- - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
- {
- if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) { }
- return self;
- }
- - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
- [super setSelected:selected animated:animated];
- }
- - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
- [[self delegate] performSelector:@selector(showMenu:)
- withObject:self afterDelay:0.9f];
- [super setHighlighted:highlighted animated:animated];
- }
- - (BOOL)canBecomeFirstResponder {
- return YES;
- }
- - (BOOL)canPerformAction:(SEL)action withSender:(id)sender{
- if (action == @selector(cut:)){
- return NO;
- } else if(action == @selector(copy:)){
- return YES;
- }
- else if(action == @selector(paste:)){
- return NO;
- }
- else if(action == @selector(select:)){
- return NO;
- }
- else if(action == @selector(selectAll:)){
- return NO;
- } else {
- return [super canPerformAction:action withSender:sender];
- }
- }
- - (void)copy:(id)sender {
- UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
- [pasteboard setString:[[self textLabel]text]];
- }
- - (void)dealloc {
- [super dealloc];
- }
- @end
Define CopyPasteTextController to implement the paste function.
- @ Interface CopyPasteTextController: UIViewController <UITableViewDelegate>
- {
- // Used to identify whether a shortcut menu is displayed
- BOOL menuVisible;
- UITableView * tableView;
- }
- @ Property (nonatomic, getter = isMenuVisible) BOOL menuVisible;
- @ Property (nonatomic, retain) IBOutlet UITableView * tableView;
- @ End
Implement CopyPasteTextController:
- # Import "CopyPasteTextController. h"
- # Import "CopyTableViewCell. h"
- @ Implementation CopyPasteTextController
- @ Synthesize menuVisible, tableView;
- -(Void) viewDidLoad {
- [Super viewDidLoad];
- [Self setTitle: @ "copy and paste text"]; // click this button to paste the clipboard content to the title.
- UIBarButtonItem * addButton = [[UIBarButtonItem alloc]
- InitWithBarButtonSystemItem: UIBarButtonSystemItemRefresh
- Target: self
- Action: @ selector (readFromPasteboard :)]
- Autorelease];
- [[Self navigationItem] setRightBarButtonItem: addButton];
- }
- // Customize the number of functions in the table view.
- -(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {
- Return 1;
- }
- -(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {
- Return 9;} // Customize the appearance of table view cells.
- -(UITableViewCell *) tableView :( UITableView *) tableView
- CellForRowAtIndexPath :( NSIndexPath *) indexPath {
- Static NSString * CellIdentifier = @ "Cell ";
- CopyTableViewCell * cell = (CopyTableViewCell *)
- [TableView dequeueReusableCellWithIdentifier: CellIdentifier];
- If (cell = nil ){
- Cell = [[[CopyTableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier] autorelease];
- [Cell setDelegate: self];
- }
- // Configure the cell.
- NSString * text = [NSString stringWithFormat: @ "Row % d", [indexPath row];
- [[Cell textLabel] setText: text];
- Return cell;
- }
- -(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {
- If ([self isMenuVisible]) {
- Return;
- }
- [[Self tableView] cellForRowAtIndexPath: indexPath] setSelected: YES
- Animated: YES];} // display menu-(void) showMenu :( id) cell {
- If ([cell isHighlighted]) {
- [Cell becomeFirstResponder];
- UIMenuController * menu = [UIMenuController sharedMenuController];
- [Menu setTargetRect: [cell frame] inView: [self view];
- [Menu setMenuVisible: YES animated: YES];
- }
- }
- -(Void) readFromPasteboard :( id) sender {
- [Self setTitle: [NSString stringWithFormat: @ "Pasteboard =%@", [[UIPasteboard generalPasteboard] string];
- }
- -(Void) didReceiveMemoryWarning {
- // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning];
- // Relinquish ownership any cached data, images, etc that aren't in use.
- }
- -(Void) viewDidUnload {
- [Super viewDidUnload];
- [Self. tableView release];
- // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
- // For example: self. myOutlet = nil;
- }
Effect:
Copy a row of data:
Click the paste button in the upper right corner to display the data on the title: