iOS開發應用剪貼簿功能教程(1)

來源:互聯網
上載者:User

iOS開發應用剪貼簿功能教程是本文要介紹的內容,在iOS中,可以使用剪貼簿實現應用程式之中以及應用程式之間實現資料的共用。比如你可以從iPhone QQ複製一個url,然後粘貼到safari瀏覽器中查看這個連結的內容。

一、在iOS中下面三個控制項,自身就有複製-粘貼的功能:

1、UITextView

2、UITextField

3、UIWebView

二、UIKit framework提供了幾個類和協議方便我們在自己的應用程式中實現剪貼簿的功能。

1、UIPasteboard:我們可以向其中寫入資料,也可以讀取資料

2、UIMenuController:顯示一個捷徑功能表,用來複製、剪貼、粘貼選擇的項。

3、UIResponder中的 canPerformAction:withSender:用於控制哪些命令顯示在捷徑功能表中。

4、當捷徑功能表上的命令點擊的時候,UIResponderStandardEditActions將會被調用。

三、下面這些項能被放置到剪貼簿中

1、UIPasteboardTypeListString —  字串數組, 包含kUTTypeUTF8PlainText

2、UIPasteboardTypeListURL —   URL數組,包含kUTTypeURL

3、UIPasteboardTypeListImage —   圖形數組, 包含kUTTypePNG 和kUTTypeJPEG

4、UIPasteboardTypeListColor —   顏色數組

四、剪貼簿的類型分為兩種:

系統級:使用UIPasteboardNameGeneral和UIPasteboardNameFind建立,系統級的剪貼簿,當應用程式關閉,或者卸載時,資料都不會丟失。

應用程式級:通過設定,可以讓資料在應用程式關閉之後仍然儲存在剪貼簿中,但是應用程式卸載之後資料就會失去。我們可用通過pasteboardWithName:create:來建立。

瞭解這些之後,下面通過一系列的例子來說明如何在應用程式中使用剪貼簿。

例子:

1、複製剪貼文本。

下面通過一個例子,可以在tableview上顯示一個捷徑功能表,上面只有複製按鈕,複製tableview上的資料之後,然後粘貼到title上。

定義一個儲存格類CopyTableViewCell,在這個類的上顯示捷徑功能表,實現複製功能。

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

實現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 

定義CopyPasteTextController,實現粘貼功能。

 
  1. @interface CopyPasteTextController : UIViewController<UITableViewDelegate>   
  2. {      
  3. //用來標識是否顯示捷徑功能表      
  4. BOOL menuVisible;      
  5. UITableView *tableView;  
  6. }  
  7. @property (nonatomic, getter=isMenuVisible) BOOL menuVisible;  
  8. @property (nonatomic, retain) IBOutlet UITableView *tableView;  
  9. @end  

實現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:@"文字複製粘貼"]; //點擊這個按鈕將剪貼簿的內容粘貼到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 sections 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];}//顯示菜單- (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;  

效果:

複製一行資料:

點擊右上方的按鈕粘貼,將資料顯示在title上:


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.