子类工具栏
Subclass tool bar
我在键盘顶部添加了一个工具栏,每次用户单击 TextView 时它都会出现,它还有许多按钮可以更改并与 TextView 交互,例如更改文本字体.
我现在想做的是创建一个 Toolbar 的子类,以便它可以在其他 TextView 上使用,但我的问题是:
如何从 Toolbar 子类中使用 TextView 委托方法?
这是我当前的代码(不是所有代码,为了不让 post 太大):
.h
#import <UIKit/UIKit.h>
@interface CustomUIToolbar : UIToolbar{
UIButton *btn1;
UIButton *btn2;
UIButton *btn3;
}
.m
@implementation CustomUIToolbar
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UIBarButtonItem *one = ...
UIBarButtonItem *two = ...
UIBarButtonItem *three = ...
self.barStyle = UIBarStyleDefault;
self.layer.borderWidth = 1;
self.layer.borderColor = [[UIColor lightGrayColor] CGColor];
self.items = [NSArray arrayWithObjects:one,placeholder,two,three,four,five,nil];
[self sizeToFit];
}
return self;
}
-(void)actionBtn3:(UIButton *) barBtnItem{
...
}
-(void)actionBtn2:(UIButton *) barBtnItem{
...
}
-(void)actionBtn1:(UIButton *) barBtnItem{
...
}
// And the UITextView Delegate Methods
- (BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
...
return YES;
}
- (void)textViewDidChangeSelection:(UITextView *)textView {
...
}
您可以为文本视图创建另一个 class 并在其中导入工具栏 class 并从此 class 调用栏按钮单击方法。并在需要的地方使用此 class 的文本视图。
您的 CustomUIToolbar class 应符合 UITextViewDelegate 协议:
@interface CustomUIToolbar : UIToolbar <UITextViewDelegate> {
然后您需要将 CustomUIToolbar subclass 指定为 textView 的委托:
self.textView.delegate = self.customTabbar;
更新:
我认为这个问题是因为你的工具栏实例在委托方法被调用之前被释放了。尝试创建一个 属性 以确保该变量保留在内存中。所以代替:
UIToolbar *tool = [[CustomUIToolbar alloc] init];
创建 属性:
@property(nonatomic, strong) CustomUIToolbar *tool;
并初始化它:
self.tool = [[CustomUIToolbar alloc] init];
我在键盘顶部添加了一个工具栏,每次用户单击 TextView 时它都会出现,它还有许多按钮可以更改并与 TextView 交互,例如更改文本字体.
我现在想做的是创建一个 Toolbar 的子类,以便它可以在其他 TextView 上使用,但我的问题是:
如何从 Toolbar 子类中使用 TextView 委托方法?
这是我当前的代码(不是所有代码,为了不让 post 太大):
.h
#import <UIKit/UIKit.h>
@interface CustomUIToolbar : UIToolbar{
UIButton *btn1;
UIButton *btn2;
UIButton *btn3;
}
.m
@implementation CustomUIToolbar
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UIBarButtonItem *one = ...
UIBarButtonItem *two = ...
UIBarButtonItem *three = ...
self.barStyle = UIBarStyleDefault;
self.layer.borderWidth = 1;
self.layer.borderColor = [[UIColor lightGrayColor] CGColor];
self.items = [NSArray arrayWithObjects:one,placeholder,two,three,four,five,nil];
[self sizeToFit];
}
return self;
}
-(void)actionBtn3:(UIButton *) barBtnItem{
...
}
-(void)actionBtn2:(UIButton *) barBtnItem{
...
}
-(void)actionBtn1:(UIButton *) barBtnItem{
...
}
// And the UITextView Delegate Methods
- (BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
...
return YES;
}
- (void)textViewDidChangeSelection:(UITextView *)textView {
...
}
您可以为文本视图创建另一个 class 并在其中导入工具栏 class 并从此 class 调用栏按钮单击方法。并在需要的地方使用此 class 的文本视图。
您的 CustomUIToolbar class 应符合 UITextViewDelegate 协议:
@interface CustomUIToolbar : UIToolbar <UITextViewDelegate> {
然后您需要将 CustomUIToolbar subclass 指定为 textView 的委托:
self.textView.delegate = self.customTabbar;
更新:
我认为这个问题是因为你的工具栏实例在委托方法被调用之前被释放了。尝试创建一个 属性 以确保该变量保留在内存中。所以代替:
UIToolbar *tool = [[CustomUIToolbar alloc] init];
创建 属性:
@property(nonatomic, strong) CustomUIToolbar *tool;
并初始化它:
self.tool = [[CustomUIToolbar alloc] init];