-1

我现在没有收到错误,但我的代表没有工作。我正在制作一个自定义键盘,所以我有 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
4

1 回答 1

2

我怀疑:

1)您的按钮的目标是笔尖的文件所有者,而不是 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)您分配一个几乎不使用的对象,因为您立即用笔尖的对象替换它

B) 从 nib ( [nibObjects objectAtIndex:0]) 中提取对象的方法确实不可靠。您应该在其中创建一个 IBOutletPageViewController并将其链接到 nib

我认为你应该重新审视你在这里使用笔尖的方式。

最后一点(不相关):你为什么使用[NSString stringWithFormat:@"..."]而不是 just @"..."

于 2012-07-27T01:46:09.103 回答