gpt4 book ai didi

ios - 子类工具栏

转载 作者:行者123 更新时间:2023-11-29 01:05:02 25 4
gpt4 key购买 nike

我在键盘顶部添加了一个工具栏,每次用户单击 TextView 时它都会出现,它还有许多按钮可以更改并与 TextView 交互,例如更改文本字体。

我现在想做的是创建一个 Toolbar 的子类,以便它可以在其他 TextView 上使用,但我的问题是:

如何从 Toolbar 子类中使用 TextView 委托(delegate)方法?

这是我当前的代码(不是所有代码,以免使帖子太大):

.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 {
...
}

最佳答案

您的 CustomUIToolbar 类应符合 UITextViewDelegate 协议(protocol):

@interface CustomUIToolbar : UIToolbar <UITextViewDelegate> {

然后您需要将您的 CustomUIToolbar 子类指定为 textView 的委托(delegate):

self.textView.delegate = self.customTabbar;

更新:

我认为这个问题是因为你的工具栏实例在委托(delegate)方法被调用之前就被释放了。尝试创建一个属性以确保该变量保留在内存中。所以不是:

UIToolbar *tool = [[CustomUIToolbar alloc] init];

创建属性:

@property(nonatomic, strong) CustomUIToolbar *tool;

并初始化它:

self.tool = [[CustomUIToolbar alloc] init];

关于ios - 子类工具栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36514306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com