Examples of usage of Uimenucontroller
Today finally understand the uimenucontroller display of the relevant content, the source code to share to everyone!
To display the menu normally, you must do the following:
1.-(BOOL) Canbecomefirstresponder must return Yes
2.-(BOOL) Canperformaction: (SEL) Action Withsender: (ID) sender
In this function, the method to display the menu item (including the system's menu item) must return Yes
3. Before the menu is displayed, you must call:
[Self Becomefirstresponder]
Be the first responder
4. In order to display the second menu normally immediately, you must use:
[Menucontroller Setmenuvisible:no];
Close it first, or it won't show! This is I finally found!!! Well, let's encourage it.
In iOS, you can apply the Clipboard to implement data sharing between the application and the application. For example, you can copy a URL from iphone qq and paste it into the Safari browser to see the content of the link.
Overview
The following three controls in iOS have the ability to copy-paste themselves:
1, Uitextview
2, Uitextfield
3, UIWebView
The UIKit framework provides several classes and peace talks to facilitate our ability to implement the Clipboard in our own application.
1, Uipasteboard: We can write to this data, can also read the data
2. Uimenucontroller: Displays a shortcut menu for copying, clipping, and pasting selected items.
3, Uiresponder in the Canperformaction:withsender: used to grasp which orders are displayed in the shortcut menu.
4. When the command clicks on the shortcut menu, the uiresponderstandardeditactions will be called.
The following items can be placed on the Clipboard
1, uipasteboardtypeliststring-string array, including Kuttypeutf8plaintext
2, Uipasteboardtypelisturl-url array, including Kuttypeurl
3. uipasteboardtypelistimage-graphic array, including Kuttypepng and Kuttypejpeg
4. uipasteboardtypelistcolor-Color Array
The type of clipboard is divided into two types:
System level: Application Uipasteboardnamegeneral and Uipasteboardnamefind, system-level application closed, or unloaded data will not be lost.
Application level: Through the process settings, you can keep the data in the Clipboard after the application is closed, but the data will be lost after the application is unloaded. We can create them through the process pasteboardwithname:create:
Having understood this, the following is a series of examples of procedures to affirm the application of the Clipboard in the application.
Example:
One, copy the clip text.
Following an example of the process, you can display a shortcut menu on TableView, with the Copy button above, copy the data on the TableView, and paste it on the title.
Defines a unit class Copytableviewcell, which displays a shortcut menu on this class, implementing the copy function.
@interface Copytableviewcell:uitableviewcell {
ID delegate;
}
@property (nonatomic, retain) ID delegate;
@end
Implement Copytableviewcell, implement paste:
#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) ED animated: (BOOL) Animated {
[Super Setselected:ed animated:animated];
}
-(void) sethighlighted: (BOOL) highlighted animated: (BOOL) Animated {
[Self delegate] Performselector:@or (showmenu:)
Withobject:self afterdelay:0.9f];
[Super sethighlighted:highlighted animated:animated];
}
-(BOOL) Canbecomefirstresponder
{
return YES;
}
-(BOOL) Canperformaction: (SEL) Action Withsender: (ID) sender{
if (action = = @or (cut:)) {
return NO;
}
else if (action = = @or (copy:)) {
return YES;
}
else if (action = = @or (paste:)) {
return NO;
}
else if (action = = @or (:)) {
return NO;
}
else if (action = = @or (all:)) {
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
@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:@ "text copy and paste"];
Click this button to paste the contents of the Clipboard into the title
Uibarbuttonitem *addbutton = [[[Uibarbuttonitem Alloc]
Initwithbarbuttonsystemitem:uibarbuttonsystemitemrefresh
Target:self
Action:@or (Readfrompasteboard:)]
Autorelease];
[[Self Navigationitem] setrightbarbuttonitem:addbutton];
}
Customize the number of sections 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];
}
Show 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 has a superview.
[Super didreceivememorywarning];
Relinquish ownership any cached data, images, etc-aren "" T in use.
}
-(void) viewdidunload
{
[Super Viewdidunload];
[Self.tableview release];
Relinquish ownership of anything that can is recreated in viewdidload or on demand.
for example:self.myOutlet = nil;
}
Results:
To copy a row of data:
Click the button in the top right corner to paste and display the data on the title:
Second, picture copy and paste
The following is an example of a process that copies and clips pictures to another Uiimageview center.
1, put two uiimageview on the interface, one is the data source of the picture, one is to paste the picture to the premises. The Copypasteimageviewcontroller code is as follows:
@interface Copypasteimageviewcontroller:uiviewcontroller {
Uiimageview *imageview;
Uiimageview *pasteview;
Uiimageview *edview;
}
@property (nonatomic, retain) Iboutlet Uiimageview *imageview;
@property (nonatomic, retain) Iboutlet Uiimageview *pasteview;
@property (nonatomic, retain) Uiimageview *edview;
-(void) Placeimageonpasteboard: (id) view;
@end
2. When we touch the picture, we display the shortcut menu:
-(void) Touchesbegan: (nsset*) touches withevent: (uievent*) Event {
Nsset *copytouches = [Event Touchesforview:imageview];
Nsset *pastetouches = [Event Touchesforview:pasteview];
[Self becomefirstresponder];
if ([copytouches count] > 0) {
[Self Performselector:@or (showmenu:)
Withobject:imageview afterdelay:0.9f];
}
else if ([pastetouches count] > 0) {
[Self Performselector:@or (showmenu:)
Withobject:pasteview afterdelay:0.9f];
}
[Super Touchesbegan:touches Withevent:event];
}
-(void) ShowMenu: (id) View {
[Self setselectedview:view];
Uimenucontroller * menu = [Uimenucontroller Sharedmenucontroller];
[Menu Settargetrect:cgrectmake (5, 1, 1) Inview:view];
[Menu Setmenuvisible:yes Animated:yes];
}
The shortcut menu here shows three menu items: clip, paste, copy:
-(BOOL) Canperformaction: (SEL) Action Withsender: (ID) sender{
if (action = = @or (cut:)) {
return ([self edview] = = ImageView)? Yes:no;
} else if (action = = @or (copy:)) {
return ([self edview] = = ImageView)? Yes:no;
} else if (action = = @or (paste:)) {
return ([self edview] = = Pasteview)? Yes:no;
} else if (action = = @or (:)) {
return NO;
} else if (action = = @or (all:)) {
return NO;
} else {
return [Super Canperformaction:action Withsender:sender];
}
}
-(void) Cut: (ID) Sender {
[Self copy:sender];
[ImageView Sethidden:yes];
}
-(void) copy: (ID) Sender {
[Self placeimageonpasteboard:[self ImageView]];
}
-(void) paste: (ID) Sender {
Uipasteboard *apppasteboard =
[Uipasteboard pasteboardwithname:@ "Copypasteimage" create:yes];
NSData *data =[apppasteboard dataforpasteboardtype:@ "Com.marizack.CopyPasteImage.imageView"];
Pasteview.image = [UIImage imagewithdata:data];
}
Results:
1. Click on the image to display the menu button.
2. Click Copy to copy the data to the Clipboard:
3, click Paste, paste the data into the Uiimageview.
Original link: http://blog.csdn.net/zhuqilin0/article/details/6661044
@interface Viewcontroller ()
@property (Weak, nonatomic) Iboutlet Uitextfield *inputtf;
-(Ibaction) Cutaction: (UIButton *) sender;
-(Ibaction) Copyaction: (UIButton *) sender;
-(Ibaction) Pasteaction: (UIButton *) sender;
-(Ibaction) Mianban: (UIButton *) sender;
@property (Weak, nonatomic) Iboutlet UILabel *textlabel;
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
Do any additional setup after loading the view, typically from a nib.la
UILabel *label = [[UILabel alloc] Initwithframe:cgrectmake (10, 10, 50, 100)];
[Self.view Addsubview:label];
Label.text = @ "123456789";
}
-(void) didreceivememorywarning {
[Super didreceivememorywarning];
Dispose of any resources the can be recreated.
}
-(Ibaction) Cutaction: (UIButton *) Sender {
Uipasteboard *pasteboard = [Uipasteboard Generalpasteboard];
pasteboard.string = Self.inputTF.text;
Self.inputTF.text = @ "";
}
-(Ibaction) Copyaction: (UIButton *) Sender {
Uipasteboard *pasteboard = [Uipasteboard Generalpasteboard];
pasteboard.string = Self.inputTF.text;
}
-(Ibaction) Pasteaction: (UIButton *) Sender {
Uipasteboard *pasteboard = [Uipasteboard Generalpasteboard];
Self.textLabel.text = pasteboard.string;
}
-(Ibaction) Mianban: (UIButton *) Sender {
Uimenucontroller * menu = [Uimenucontroller Sharedmenucontroller];
[Menu Settargetrect:cgrectmake (ten, +, +) InView: [self view]];
[Menu Setmenuvisible:no Animated:yes];
}
-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (uievent *) event{
[Self.view Endediting:yes];
}
@end
The details of iOS development clipboard