gpt4 book ai didi

ios - 为什么这个委托(delegate)不适用于 UITextView

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:39:09 26 4
gpt4 key购买 nike

- (void)viewDidLoad
{
[super viewDidLoad];

}

-(id)init{
self = [super initWithNibName:@"WritingView" bundle:nil ];
if (self) {
//self.view.delegate = self;
self.txtMain.userInteractionEnabled = YES;
self.txtMain.delegate = self;

}
return self;
}

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
return [self init];
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{

NSLog(@"textViewShouldBeginEditing:");

return YES;

}

self.txtMain 是我在 xib 中的 Root View :WritingView,我的 View Controller 实现了协议(protocol) UITextViewDelegate ,如下所示:

@interface WritingViewController : UIViewController<UITextViewDelegate>
{

}
@property (strong, nonatomic) IBOutlet UITextView *txtMain;

我已经在 textViewShouldBeginEditing 或 UITextViewDelegate 中的其他函数中设置了断点,但从未进入,为什么?顺便说一句,这个 ViewController 是由另一个 ViewController 创建的,就像这样:

WritingViewController *aViewController = [[WritingViewController alloc] initWithNibName:@"WritingView"   bundle:nil];

[self.view.superview addSubview:aViewController.view];
[self.view removeFromSuperview];

谁能告诉我为什么它不起作用,然后我更改了初始化代码:

-(id)init{
self = [super initWithNibName:@"WritingView" bundle:nil ];
if (self) {
UITextView *txtView = [[UITextView alloc] initWithFrame:self.view.frame];

txtView.textColor = [UIColor blackColor];
txtView.font = [UIFont fontWithName:@"Arial" size:18.0];
txtView.text = @"Now is the time for all good developers tocome to serve their country.\n\nNow is the time for all good developers to cometo serve their country.";//

self.txtMain = txtView;
self.txtMain.userInteractionEnabled = YES;
self.txtMain.delegate = self;
[self.view addSubview:txtView];

}
return self;
}

显然我使用了一个空白的View作为root-view,但是这次,当我点击文本时,程序在main()中creshed:

      return UIApplicationMain(argc, argv, nil, NSStringFromClass([WheelDemoAppDelegate class]));

在控制台中:

2013-11-03 22:53:57.514 Wheel demo[1718:a0b] *** -[WritingViewController respondsToSelector:]: message sent to deallocated instance 0x9b4eeb0

(数据库)

最佳答案

您需要以某种方式确保 ARC 系统不会从内存中删除您的对象。一旦对象的保留计数达到零,ARC 将删除对象,并且该对象不再在范围内。 ARC 系统与垃圾收集器非常不同。您可能想阅读它:https://developer.apple.com/library/mac/releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html

您遇到的错误是由于没有保留您的 View Controller (WritingViewController)。尝试在创建 View Controller 的类上创建一个属性:

@property (nonatomic,strong) WritingViewController *writtingVc;

并在创建后立即将其设置为您的 WritingViewController 实例。

关于ios - 为什么这个委托(delegate)不适用于 UITextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19754253/

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