我正在尝试覆盖 uitextview 类中的一个方法,我想在我正在工作的类中执行它,但我无法让它工作。我只看到人们在一个单独的类中覆盖它。谢谢你。
#import <UIKit/UIKit.h>
@interface MyTextView : UITextView
@end
@interface ViewController : UIViewController
@property (nonatomic, retain) IBOutlet MyTextView * tv;
@end
//////////////////////////////////
#import "ViewController.h"
@implementation MyTextView
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
[UIMenuController sharedMenuController].menuVisible = NO; //do not display the menu
// [self resignFirstResponder]; //do not allow the user to selected anything
return NO;
}
@end
@implementation ViewController
@synthesize tv;
- (void)viewDidLoad
{
[super viewDidLoad];
tv = [[MyTextView alloc] init];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
-(void)textViewDidChangeSelection:(MyTextView *)textView
{
NSRange range = textView.selectedRange;
if(range.location<textView.text.length)
{
NSString * getRidOfFirstHalfString = [tv.text substringFromIndex:range.location];
NSString * selectedString = [getRidOfFirstHalfString substringToIndex:range.length];
NSLog(@"%@",selectedString);
}
else
{
// Do what ever you want to do
}
}
@end