我现在没有收到错误但是我的代表没有工作。我正在制作一个自定义键盘,所以我有UIViewController和UIView。我希望UIView在UIViewController中调用sendKeyboardShortCut方法。未调用sendKeyboardShortCut方法。谢谢
// ViewController .h
#import "KeyboardExtension.h"
@interface PageViewController : UIViewController <UITextViewDelegate,sendKeyboardShortCutDelegate> {
KeyboardExtension *inputAccView;
}
-(void)sendKeyboardShortCut:(NSString*)shortCut;
@property (nonatomic, assign) IBOutlet UITextView *tv;
@end
// ViewController .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
答案 0 :(得分:2)
我怀疑:
1)你的按钮的目标是nib的文件所有者而不是KeyboardExtension
2)您的操作设置为不发送发件人,因此UIKit调用-comma而不是-comma:
此外,以下代码非常可疑:
inputAccView = [[[KeyboardExtension alloc] init]autorelease];
inputAccView.delegate = self;
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"KeyboardExtension" owner:self options:nil];
inputAccView = [nibObjects objectAtIndex:0];
A)你分配一个你几乎不用的对象,当你用nib的对象立即替换它时
B)从nib([nibObjects objectAtIndex:0]
)中提取对象的方法实际上并不健全。您应该在PageViewController
中创建一个IBOutlet并将其链接到nib
我认为你应该重新审视你在这里使用笔尖的方式。
最终(无关)点:您为什么使用[NSString stringWithFormat:@"..."]
而不只是@"..."
?