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

ios - NSNotificationCenter 是 UITextView 的一部分吗?

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

对于这个基本问题,我很抱歉,我对编程很陌生,并试图理解代码苹果建议器中的某些内容,以便为我想要执行的某些内容提供某种解决方案。

我创建了一个简单的笔记应用程序,非常基本,目前我有: 1. CreateNote View Controller 2. NotesList TableView Controller

所以我想在创建笔记并且用户在键盘下方键入时添加一种行为,以便调整文本大小,以便用户仍然可以看到他键入的内容并且文本不会在键盘后面。

所以我添加了一些苹果建议的代码行来完成。

viewWillAppear 中调用了 NSNotificationCenter 上的一个方法,我无法理解在哪里声明了 NSNotificationCenter 对象...?

这是我的 CreateNote View Controller (请帮助我理解为什么他们可以执行此调用):

#import "NMCreateNotesViewController.h"


@interface NMCreateNotesViewController () <UITextViewDelegate>

@property (weak, nonatomic) IBOutlet UIBarButtonItem *createButton;

@property (weak, nonatomic) IBOutlet UITextView *textField;

@end



@implementation NMCreateNotesViewController


- (void)viewWillAppearBOOL)animated
{
    [super viewWillAppear:animated];

    // listen for keyboard hide/show notifications so we can properly adjust the table's height
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selectorselector(keyboardWillShow
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selectorselector(keyboardWillHide
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}


#pragma mark - Notifications


- (void)adjustViewForKeyboardRevealBOOL)showKeyboard notificationInfoNSDictionary *)notificationInfo
{
    // the keyboard is showing so resize the table's height
    CGRect keyboardRect = [[notificationInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    NSTimeInterval animationDuration =
    [[notificationInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGRect frame = self.textField.frame;

    // the keyboard rect's width and height are reversed in landscape
    NSInteger adjustDelta = UIInterfaceOrientationIsPortrait(self.interfaceOrientation) ? CGRectGetHeight(keyboardRect) : CGRectGetWidth(keyboardRect);

    if (showKeyboard)
        frame.size.height -= adjustDelta;
    else
        frame.size.height += adjustDelta;

    [UIView beginAnimations"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    self.textField.frame = frame;
    [UIView commitAnimations];
}



- (void)keyboardWillShowNSNotification *)aNotification
{
    [self adjustViewForKeyboardReveal:YES notificationInfo:[aNotification userInfo]];
}



- (void)keyboardWillHideNSNotification *)aNotification
{
    [self adjustViewForKeyboardReveal:NO notificationInfo:[aNotification userInfo]];
}



- (void) prepareForSegueUIStoryboardSegue *)segue senderid)sender
{
    if (sender != self.createButton) return;
    if (self.textField.text.length > 0) {
        self.note = [[NMNote alloc] init];
        self.note.content = self.textField.text;

    }
}



- (id)initWithNibNameNSString *)nibNameOrNil bundleNSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}



- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end



Best Answer-推荐答案


Is NSNotificationCenter is part of UITextView?

不,不是。 NSNotificationCenter 顾名思义是一个通知中心。对象可以使用 NSNotificationCenter 订阅通知和发布通知,以处理和通知某些事件。

他们使用 NSNotificationCenter 让 viewcontroller 订阅 UIKeyboardWillHideNotification 和 UIKeyboardWillShowNotification 事件。

看看这个:

    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selectorselector(keyboardWillShow
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

NSNotificationCenter 被设计为用作单例(我相信这是正确的术语,如果我错了,请纠正我)所以我们通过调用类方法 defaultCenter 来访问这个应用程序进程的 NSNotificationCenter。它添加了观察者'self'(在这种情况下是 View Controller 的一个实例),并且基本上指示它在触发 UIKeyboardWillShowNotification 名称下的通知时向观察者发送消息keyboardWillShow。

什么对象会触发 UIKeyboardWillShowNotification?好吧,它不是 UITextView,这个通知名称实际上是在 UIWindow.h 中定义的,所以它可能来自那里,而它又可能是从 UIKeyboard 调用的,据我所知,它不是公共(public) API。

关于ios - NSNotificationCenter 是 UITextView 的一部分吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22825126/

回复

使用道具 举报

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

本版积分规则

关注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