• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-12 16:08:44 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

- (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)initWithNibNameNSString *)nibNameOrNil bundleNSBundle *)nibBundleOrNil{
    return [self init];
}

- (BOOL)textViewShouldBeginEditingUITextView *)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;

我在 UITextViewDelegate 的 textViewShouldBeginEditing 或其他函数中设置了断点,但为什么从来没有进入? 顺便说一下,这个 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() 中崩溃了:

      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

(lldb)



Best Answer-推荐答案


您需要以某种方式确保 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap