I am not getting an error now but my delegate is not working. I am making a custom keyboard so i have the UIViewController and a UIView. I want the UIView to call the sendKeyboardShortCut method in the UIViewController. The sendKeyboardShortCut method is not being called. Thank You
//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
//keyboard view .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
//keyboard view .m
#import "KeyboardExtension.h"
@implementation KeyboardExtension
@synthesize delegate;
-(IBAction)dash:(id)sender{[delegate sendKeyboardShortCut:@"dash"];}
@end
I suspect that:
1) your button’s target is the nib’s file owner and not KeyboardExtension
2) your action is setup not to send the sender, so UIKit calls -comma instead of -comma:
Also, the following code is highly suspect:
A) you allocate an object that you barely use as you replace it immediately with the nib’s object
B) the way to extract the object from the nib (
[nibObjects objectAtIndex:0]) is really not robust. You should create an IBOutlet inPageViewControllerand link it in the nibI think you should revisit the way you use nibs here.
A final (unrelated) point: why are you using
[NSString stringWithFormat:@"..."]instead of just@"..."?