How to simulate the cut copy and paste functions in code with pasteboard? Here is a simple example for that.
1. Start a new xcode project.
2. Add two textviews (textpad and notepad in my example!) and three buttons as below.
Here is the interface code.
1. Start a new xcode project.
2. Add two textviews (textpad and notepad in my example!) and three buttons as below.
Here is the interface code.
//
// PasteBoardSampleViewController.h
// PasteBoardSample
//
// Created by Raja T S Sekhar on 3/24/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface PasteBoardSampleViewController : UIViewController {
IBOutlet UITextView *textPad;
IBOutlet UITextView *notePad;
}
@property (nonatomic, retain) UITextView *textPad;
@property (nonatomic, retain) UITextView *notePad;
-(IBAction)copy;
-(IBAction)cut;
-(IBAction)paste;
@end
Here is the implementation
//
// PasteBoardSampleViewController.m
// PasteBoardSample
//
// Created by Raja T S Sekhar on 3/24/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "PasteBoardSampleViewController.h"
@implementation PasteBoardSampleViewController
@synthesize textPad;
@synthesize notePad;
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
-(IBAction)cut {
[self copy];
textPad.text = @"";
}
-(IBAction)copy {
NSString *copyString = [[NSString alloc] initWithFormat:@"%@",[textPad text]];
UIPasteboard *pb = [UIPasteboard generalPasteboard];
[pb setString:copyString];
}
-(IBAction)paste {
UIPasteboard *pb = [UIPasteboard generalPasteboard];
notePad.text = [pb string];
}
Run the application.
Type something first text view. Now use the buttons to copy/cut and paste the text to other. The code uses pasteboard for the process.
0 comments:
Post a Comment