take a look at the following Uipasteboard usage-(void) Writevalueinappone {uipasteboard*myuipasteboard =[Uipasteboard Generalpasteboard]; printf ("myuipasteboard.string%s\n", [Myuipasteboard.stringutf8string]); Myuipasteboard.string=MyLabel.Text;} -(void) getvalueinapptwo {uipasteboard*myuipasteboard =[Uipasteboard Generalpasteboard]; printf ("myuipasteboard.string%s\n", [Myuipasteboard.stringutf8string]); Mytextfield.text= Myuipasteboard.string; }
2. Detailed Usage: http://blog.csdn.net/zhangao0086/article/details/7580654 (ext.)
Sometimes we may need to copy the text on the Uilabel, or the Uiimageview picture, and Uilabel and Uiimageview do not respond to the touch event or copy, then we need to implement a replicable uilabel ourselves. Add a new class to inherit from Uilabel:
- @interface Uicopylabel:uilabel
- @end
- #import "UICopyLabel.h"
- @implementation Uicopylabel
- @end
In order to receive the event (which can be the first responder), we need to override a method:
- -(BOOL) canbecomefirstresponder{
- return YES;
- }
There are also two methods that need to be covered for replication operations:
- Features of "Feedback" care
- -(BOOL) Canperformaction: (SEL) Action Withsender: (ID) sender{
- return (Action = = @selector (copy:));
- }
- Implementation for copy
- -(void) copy: (ID) sender{
- Uipasteboard *pboard = [Uipasteboard Generalpasteboard];
- pboard.string = Self.text;
- }
With the above three methods, we can handle copy, of course, in case of receiving the event:
- Uilabel The default is not to receive events, we need to add the touch event ourselves
- -(void) attachtaphandler{
- self.userinteractionenabled = YES; //master switch for user interaction
- UITapGestureRecognizer *touch = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (Handletap:)];
- touch.numberoftapsrequired = 2;
- [Self addgesturerecognizer:touch];
- [Touch release];
- }
- Binding events
- -(ID) initWithFrame: (CGRect) frame
- {
- self = [super Initwithframe:frame];
- if (self) {
- [Self attachtaphandler];
- }
- return self;
- }
- Ditto
- -(void) awakefromnib{
- [Super awakefromnib];
- [Self attachtaphandler];
- }
We are ready to receive the event! Since I set the tap number to 2 above, so I need to double-click to capture, next, we need to deal with this tap, so that the menu bar pop out:
- -(void) Handletap: (uigesturerecognizer*) recognizer{
- [Self becomefirstresponder];
- Uimenucontroller *menu = [Uimenucontroller Sharedmenucontroller];
- [Menu SetTargetRect:self.frame InView:self.superview];
- [Menu Setmenuvisible:yes Animated:yes];
- }
In this way, a replicable Uilabel is born! It can handle receive clicks, pop-up menu bars, handle copy, which is a very common replicable control.
-----------------------------------------the wretched dividing line-----------------------------------------
Next we make a replicable uiimageview, create a new Viewcontroller, put two imageview, and show different graphs by default:
Then copy the above code directly over to three places:
- -(BOOL) Canperformaction: (SEL) Action Withsender: (ID) sender{
- return (Action = = @selector (copy:) | | action = = @selector (paste:));
- }
- -(void) copy: (ID) sender{
- Uipasteboard *pboard = [Uipasteboard Generalpasteboard];
- Pboard.image = Self.image;
- }
- -(void) paste: (ID) sender{
- Uipasteboard *pboard = [Uipasteboard Generalpasteboard];
- Self.image = Pboard.image;
- }
-----------------------------------------the wretched dividing line-----------------------------------------
Uipasteboard can not only communicate within the application, but also communicate between applications, you should have seen, such as I copy a URL, and then open Safari, paste into the address bar, and we can "quietly" between the application of communication, sharing data.
Note: the "whisper" here simply says that other applications do not know, and the system is allowed.
|
We use the template single View application to create two simple works, one called Pasteboardwrite, the other called Pasteboardread, the interface is as follows:
In the Pasteboardwrite, click "Write" after the text in the TextField to the pasteboard, and then switch to Pasteboardread when the display. If we only want to give "one of our own", we can not use the system of the universal pasteboard, we need to create a:
- Need to provide a unique name, generally use the inverted domain name: com.mycompany.myapp.pboard
- The following parameter indicates whether, if not present, creates a
- Uipasteboard *PB = [Uipasteboard pasteboardwithname:@"Testboard" create:yes];
Using this clipboard, we can put the text in, and then read it in another app, some of the common types have been set to properties:
In addition, if the data types that are capable of converting to plist (NSString, Nsarray, Nsdictionary, NSDate, NSNumber, and Nsurl), we can call Setvalue:forpasteboardtype: method to store data, other types can only call Setdata:forpasteboardtype: Method (plist data type can also be used), similar to this:
- Nsdictionary *dict = [nsdictionary dictionaryWithObject:textField.text forkey:@"content"];
- NSData *dictdata = [Nskeyedarchiver archiveddatawithrootobject:dict];
- [PB setdata:dictdata forpasteboardtype:@"MyType"];
Get is similar to this:
- Uipasteboard *PB = [Uipasteboard pasteboardwithname:@"Testboard" create:yes];
- Nsdictionary *dict = [nskeyedunarchiver unarchiveobjectwithdata:[pb dataforpasteboardtype:@"MyType"]];
- Caption.text = [dict objectforkey:@"content"];
A pasteboardtype is mentioned above, which is a uniform type identifier (Uniform type Identifier UTI) that helps the app to get the data it can handle. For example, you can only handle the text paste, that gives you a uiimage is obviously useless. You can use a common UTI, or you can use any character, and Apple recommends using the inverted domain name plus the type name: Com.myCompany.myApp.myType.
With your own clipboard, you can only communicate between your local apps, and sometimes it can make your user experience even better, like that.
UPDATED:
IOS7 modified part of the design of the Uipasteboard.
Before this, you can access the content only if you know the name of any Uipasteboard, and now only the app under the same Cfbundleidentifier identity can share the content. such as Com.mycompany.a and com.mycompany.b, their com.mycompany parts are the same, they can be shared.
Transfer information between apps in iOS development 2--uipasteboard