我现在没有收到错误,但我的代表没有工作。我正在制作一个自定义键盘,所以我有 UIViewController 和 UIView。我希望 UIView 调用 UIViewController 中的 sendKeyboardShortCut 方法。未调用 sendKeyboardShortCut 方法。谢谢你
//视图控制器.h
#import "KeyboardExtension.h"
@interface PageViewController : UIViewController <UITextViewDelegate,sendKeyboardShortCutDelegate> {
KeyboardExtension *inputAccView;
}
-(void)sendKeyboardShortCut:(NSString*)shortCut;
@property (nonatomic, assign) IBOutlet UITextView *tv;
@end
//视图控制器.m
@implementation PageViewController
@synthesize tv;
- (void)viewWillAppear:(BOOL)animated
{
tv.delegate = self;
}
-(void)createInputAccessoryView{
inputAccView = [[[KeyboardExtension alloc] init]autorelease];
inputAccView.delegate = self;
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"KeyboardExtension" owner:self options:nil];
inputAccView = [nibObjects objectAtIndex:0];
}
-(void)textViewDidBeginEditing:(UITextView *)textView{
[self createInputAccessoryView];
[textView setInputAccessoryView:inputAccView];
}
-(void)sendKeyboardShortCut:(NSString*)shortCut{
if ([shortCut isEqualToString:@"dash"] ) {
NSRange range = tv.selectedRange;
if((range.location+range.length)<=tv.text.length)
{
NSString * before = [tv.text substringToIndex:range.location];
NSString * after = [tv.text substringFromIndex:range.location+range.length];
tv.text = [NSString stringWithFormat:@"%@-%@",before,after];
}
}
}
@end
//键盘视图.h
#import <UIKit/UIKit.h>
@protocol sendKeyboardShortCutDelegate <NSObject>
-(void)sendKeyboardShortCut:(NSString*)shortCut;
@end
@interface KeyboardExtension : UIView
-(IBAction)dash:(id)sender;
@property(nonatomic,assign) id<sendKeyboardShortCutDelegate>delegate;
@end
//键盘视图.m
#import "KeyboardExtension.h"
@implementation KeyboardExtension
@synthesize delegate;
-(IBAction)dash:(id)sender{[delegate sendKeyboardShortCut:@"dash"];}
@end