IOS development app clipboard function tutorial (1)

Source: Internet
Author: User

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.

 
 
  1. @interface CopyTableViewCell : UITableViewCell {     
  2.  id delegate;}@property (nonatomic, retain) id delegate;  
  3.  @end 

Implement CopyTableViewCell:

 
 
  1. #import "CopyTableViewCell.h"@implementation CopyTableViewCell@synthesize delegate;  
  2. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier   
  3. {     
  4.  if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) { }    
  5.    return self;  
  6. }  
  7. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {   
  8.    [super setSelected:selected animated:animated];  
  9. }  
  10. - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {    
  11.   [[self delegate] performSelector:@selector(showMenu:)                
  12.            withObject:self afterDelay:0.9f];         
  13.            [super setHighlighted:highlighted animated:animated];  
  14. }  
  15. - (BOOL)canBecomeFirstResponder {   
  16.    return YES;  
  17. }  
  18. - (BOOL)canPerformAction:(SEL)action withSender:(id)sender{   
  19.    if (action == @selector(cut:)){   
  20.     return NO;  
  21. }  else if(action == @selector(copy:)){      
  22.     return YES;    
  23.  }       
  24.  else if(action == @selector(paste:)){   
  25.     return NO;    
  26. }       
  27. else if(action == @selector(select:)){     
  28.      return NO;    
  29.  }      
  30.   else if(action == @selector(selectAll:)){      
  31.  return NO;    
  32. }    else  {     
  33.   return [super canPerformAction:action withSender:sender];    
  34. }  
  35. }  
  36. - (void)copy:(id)sender {    
  37.   UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];     
  38.   [pasteboard setString:[[self textLabel]text]];  
  39.  }  
  40. - (void)dealloc {    
  41.   [super dealloc];  
  42. }  
  43. @end 

Define CopyPasteTextController to implement the paste function.

 
 
  1. @ Interface CopyPasteTextController: UIViewController <UITableViewDelegate>
  2. {
  3. // Used to identify whether a shortcut menu is displayed
  4. BOOL menuVisible;
  5. UITableView * tableView;
  6. }
  7. @ Property (nonatomic, getter = isMenuVisible) BOOL menuVisible;
  8. @ Property (nonatomic, retain) IBOutlet UITableView * tableView;
  9. @ End

Implement CopyPasteTextController:

 
 
  1. # Import "CopyPasteTextController. h"
  2. # Import "CopyTableViewCell. h"
  3. @ Implementation CopyPasteTextController
  4. @ Synthesize menuVisible, tableView;
  5. -(Void) viewDidLoad {
  6. [Super viewDidLoad];
  7. [Self setTitle: @ "copy and paste text"]; // click this button to paste the clipboard content to the title.
  8. UIBarButtonItem * addButton = [[UIBarButtonItem alloc]
  9. InitWithBarButtonSystemItem: UIBarButtonSystemItemRefresh
  10. Target: self
  11. Action: @ selector (readFromPasteboard :)]
  12. Autorelease];
  13. [[Self navigationItem] setRightBarButtonItem: addButton];
  14. }
  15. // Customize the number of functions in the table view.
  16. -(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {
  17. Return 1;
  18. }
  19. -(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {
  20. Return 9;} // Customize the appearance of table view cells.
  21. -(UITableViewCell *) tableView :( UITableView *) tableView
  22. CellForRowAtIndexPath :( NSIndexPath *) indexPath {
  23. Static NSString * CellIdentifier = @ "Cell ";
  24. CopyTableViewCell * cell = (CopyTableViewCell *)
  25. [TableView dequeueReusableCellWithIdentifier: CellIdentifier];
  26. If (cell = nil ){
  27. Cell = [[[CopyTableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier] autorelease];
  28. [Cell setDelegate: self];
  29. }
  30. // Configure the cell.
  31. NSString * text = [NSString stringWithFormat: @ "Row % d", [indexPath row];
  32. [[Cell textLabel] setText: text];
  33. Return cell;
  34. }
  35. -(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {
  36. If ([self isMenuVisible]) {
  37. Return;
  38. }
  39. [[Self tableView] cellForRowAtIndexPath: indexPath] setSelected: YES
  40. Animated: YES];} // display menu-(void) showMenu :( id) cell {
  41. If ([cell isHighlighted]) {
  42. [Cell becomeFirstResponder];
  43. UIMenuController * menu = [UIMenuController sharedMenuController];
  44. [Menu setTargetRect: [cell frame] inView: [self view];
  45. [Menu setMenuVisible: YES animated: YES];
  46. }
  47. }
  48. -(Void) readFromPasteboard :( id) sender {
  49. [Self setTitle: [NSString stringWithFormat: @ "Pasteboard =%@", [[UIPasteboard generalPasteboard] string];
  50. }
  51. -(Void) didReceiveMemoryWarning {
  52. // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning];
  53. // Relinquish ownership any cached data, images, etc that aren't in use.
  54. }
  55. -(Void) viewDidUnload {
  56. [Super viewDidUnload];
  57. [Self. tableView release];
  58. // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
  59. // For example: self. myOutlet = nil;
  60. }

Effect:

Copy a row of data:

Click the paste button in the upper right corner to display the data on the title:


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.