Beginning with iPhone OS 3.0, users can now copy text, images, or other data in one application and paste that data to another location within the same application or in a different application. You can, for example, copy a person’s address in an email message and paste it into the appropriate field in the Contacts application. The UIKit framework currently implements copy-cut-paste in the UITextView, UITextField, and UI WebView classes. If you want this behavior in your own applications, you can either use objects of these classes or implement copy cut paste yourself.
The following sections describe the programmatic interfaces of the UIKit that you use for copy, cut, and paste operations and explain how they are used.
UIKit Facilities for Copy-Paste Operations
Several classes and an informal protocol of the UIKit framework give you the methods and mechanisms you need to implement copy, cut, and paste operations in your application:
A pasteboard is a standardized mechanism for exchanging data within applications or between applications. The most familiar use for pasteboards is handling copy, cut, and paste operations:
In iPhone OS, a pasteboard is also used to support Find operations. Additionally, you may use pasteboards to transfer data between applications using custom URL schemes instead of copy, cut, and paste commands; see “Communicating with Other Applications” for information about this technique.
Regardless of the operation, the basic tasks you perform with a pasteboard object are to write data to a pasteboard and to read data from a pasteboard. Although these tasks are conceptually simple, they mask a number of important details. The main complexity is that there may be a number of ways to represent data, and this complexity leads to considerations of efficiency. These and other issues are discussed in the following sections.
Pasteboards may be public or private. Public pasteboard are called system pasteboards; private pasteboards are created by applications, and hence are called applicationpasteboards. Pasteboards must have unique names. UIPasteboard defines two system pasteboards, each with its own name and purpose:
Typically you use one of the system-defined pasteboards, but if necessary you can create your own application pasteboard using pasteboard With Name: create: If you invoke paste board With Unique Name, UI Pasteboard gives you a uniquely-named application pasteboard. You can discover the name of a pasteboard through its name property.
Pasteboards can be marked persistent so that they continue to exist beyond the termination of applications that use them. Pasteboards that aren’t persistent are removed when the application that created them quits. System pasteboards are persistent. Application pasteboards by default are not persistent. To make an application pasteboard persistent, set its persistent property to YES. A persistent application pasteboard is removed when a user uninstalls the owning application.
Pasteboard Owner and Items
The object that last put data onto the pasteboard is referred to as the pasteboard owner. Each piece of data placed onto a pasteboard is considered a pasteboard item. The pasteboard can hold single or multiple items. Applications can place or retrieve as many items as they wish. For example, say a user selection in a view contains both text and an image. The pasteboard lets you copy the text and the image to the pasteboard as separate items. An application reading multiple items from a pasteboard can choose to take only those items that issupports (the text, but not the image, for example).
Representations and UTIs
Pasteboard operations are often carried out between two different applications. Neither application is required to know about the other, including the kinds of data it can handle. To maximize the potential for sharing, a pasteboard can hold multiple representations of the same pasteboard item. For example, a rich text editor might provide HTML, PDF, and plain-text representations of the copied data. An item on a pasteboard includes all representations of that data item that the application can provide.
Each representation of a pasteboard item is typically identified by a Unique Type Identifier (UTI).(A UTI is simply a string that uniquely identifies a particular data type. The UTI provides a common means to identify data types. If you have a custom data type you wish to support, you must create a unique identifier for it. For this, you could use reverse-DNS notation for your representation-type string to ensure uniqueness; for example, a custom representation type could be com.myCompany.myApp.myType.
For example, suppose an application supported selection of rich text and images. It may want to place on a pasteboard both rich text and Unicode versions of a text selection and different representations of an image selection. Each representation of each item is stored with its own data,
Pasteboard items and representations
In general, to maximize the potential for sharing, pasteboard items should include as many different representations as possible.
A pasteboard reader must find the data type that best suits its capabilities (if any). Typically, this means selecting the richest type available. For example, a text editor might provide HTML (rich text) and plain-text representations of copied text data. An application that supports rich text should retrieve the HTML representation and an application that only supports plain text should retrieve the plain-text version.
The change count is a per-pasteboard variable that increments every time the contents of the pasteboard changes—specifically, when items are added, modified, or removed. By examining the change count (through the changeCount property), an application can determine whether the current data in the pasteboard is the same as the data it last received. Every time the change count is incremented, the pasteboard sends a notification to interested observers.
Selection and Menu Management
To copy or cut something in a view, that “something” must be selected. It can be a range of text, an image, a URL, a color, or any other representation of data, including customobjects. To implement copy-and-paste behavior in your custom view, you must manage the selection of objects in that view yourself. If the user selects an object in the view by making a certain touch gesture (for example, a double-tap) you must handle that event, internally record the selection (and deselect any previous selection), and perhaps visually indicate the new selection in the view. If it is possible for users to select multiple objects in your view for copy-cut-paste operations, you must implement that multiple-selection behavior.
When your application determines that the user has requested the editing menu—which could be the action of making a selection—you should complete the following steps to display the menu:
Listing illustrates how you might display the edit menu in an implementation of the touchesEnded: withEvent: method. (Note that the example omits the section of code that handles the selection.) This code snippet also shows that the custom view sending itself a become First Responder message to ensure that it is the first responder for the subsequent copy, cut, and paste operations.
Displaying the editing menu
The menu initially includes all commands for which the first responder has corresponding UI Responder Standard Edit Actions method implementations (copy:, paste:, and so on). Before the menu is displayed, however, the system sends a canPerformAction:withSender: message to the first responder, which in many cases is the custom view itself. In its implementation of this method, the responder evaluates whether the command (indicated by the selector in the first argument) is applicable in the current context. For example, if the selector is paste: and there is no data in the pasteboard of a type the view can handle, the responder should return NO to suppress the Paste command. If the first responder does not implement the can PerformAction: withSender: method, or does not handle the given command, the message travels up the responder chain.
Listing shows an implementation of the can Perform Action: withSender: method that looks for message matching the copy:, copy:, and paste: selectors; it enables or disables the Copy, Cut, and Paste menu commands based on the current selection context and, for paste, the contents of the pasteboard.
Conditionally enabling menu commands
Note that the final else clause in this method calls the superclass implementation to give any superclasses a chance to handle commands that the subclass chooses to ignore.
Note that a menu command, when acted upon, can change the context for other menu commands. For example, if the user selects all objects in the view, the Copy and Cut commands should be included in the menu. In this case the responder can, while the menu is still visible, call update on the menu controller; this results in the reinvocation of can
Perform Action: with Sender: on the first responder.
Copying and Cutting the Selection
When users tap the Copy or Cut command of the editing menu, the system invokes the copy: or cut: method (respectively) of the responder object that implements it. Usually the first responder—your custom view—implements these methods, but if the first responder doesn’t implement them, the message travels up the responder in the usual fashion. Note that the UIResponderStandardEditActions informal protocol declares these methods.
In response to a copy: or cut: message, you write the object or data represented by the selection to the pasteboard in as many different representations as you can. This operation involves the following steps (which assume a single pasteboard item):
Listing shows implementations of the copy: and cut: methods. The cut: method invokes the copy: method and then removes the selected object from the view and the data model. Note that the cut: method archives a custom object to obtain an NSData object that it can pass to the pasteboard in setData:forPasteboardType:.
Copying and cutting operations
Pasting the Selection
When users tap the Paste command of the editing menu, the system invokes the paste: method of the responder object that implements it. Usually the first responder— your custom view—implements this method, but if the first responder doesn’t implement it, the message travel up the responder in the usual fashion. The paste: method is declared by the UI Responder Standard Edit Actions informal protocol.
In response to a paste: message, you read an object from the pasteboard in a representation that your application supports. Then you add the pasted object to the application’s data model and display the new object in the view in the user-indicated location. This operation involves the following steps (which assume a single pasteboard item):
Listing is an example of an implementation of the paste: method. It does the reverse of the combined cut: and copy: methods. The custom view first sees whether the general pasteboard holds its custom representation of data; if it does, it then reads the data from the pasteboard, adds it to the application’s data model, and marks part of itself—the current selection—for redrawing.
Pasting data from the pasteboard to a selection
Dismissing the Editing Menu
When your implementation of the cut:, copy: or paste: command returns, the editing menu is automatically hidden. You can keep it visible with the following line of code:
[UIMenuController setMenuController].menuVisible = YES;
The system may hide the editing menu at any time. For example, it hides the menu when an alert is displayed or the user taps in another area of the screen. If you have state or a display that depends on whether the editing menu is visible, you should listen for the notification named UI Menu Controller Will Hide Menu Notification and take an appropriate action.
IPHONE APPS Related Interview Questions
|C Interview Questions||IPHONE APPS Interview Questions|
|Mobile Testing Interview Questions||Oracle apps Interview Questions|
|Java Native Interface (JNI) Interview Questions||JNDI (Java Naming and Directory Interface) Interview Questions|
|Mobile computing Interview Questions||jQuery Mobile Interview Questions|
|Mobile Security Interview Questions||Objective C Interview Questions|
|Mobile Developer Interview Questions||Mobile Application Testing Interview Questions|
|Apps Associates Manual Testing Interview Questions||Mobile Marketing Interview Questions|
Iphone Apps Tutorial
The Core Application
Window And Views
Graphics And Drawing
Text And Web
Files And Networking
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.