-1

現在エラーは発生していませんが、デリゲートが機能していません。カスタムキーボードを作成しているので、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
4

1 に答える 1

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)すぐにペン先のオブジェクトに置き換えるため、ほとんど使用しないオブジェクトを割り当てます

B)ペン先([nibObjects objectAtIndex:0])からオブジェクトを抽出する方法は実際には堅牢ではありません。にIBOutletを作成し、PageViewControllerそれをnibにリンクする必要があります

ここでペン先の使い方を再検討する必要があると思います。

最後の(無関係な)ポイント:なぜあなた[NSString stringWithFormat:@"..."]はただの代わりに使用しているの@"..."ですか?

于 2012-07-27T01:46:09.103 に答える