ios - NSNotificationCenter 是 UITextView 的一部分吗?

标签 ios objective-c ios7.1

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

我创建了一个简单的笔记应用程序,非常基本,目前我有: 1.CreateNote View Controller 2.NotesList表格 View 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)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

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


#pragma mark - Notifications


- (void)adjustViewForKeyboardReveal:(BOOL)showKeyboard notificationInfo:(NSDictionary *)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)keyboardWillShow:(NSNotification *)aNotification
{
    [self adjustViewForKeyboardReveal:YES notificationInfo:[aNotification userInfo]];
}



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



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

    }
}



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)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

最佳答案

Is NSNotificationCenter is part of UITextView?

不,不是。正如其名称所示,NSNotificationCenter 是一个通知中心。对象可以通过 NSNotificationCenter 订阅通知和发布通知来处理和通知某些事件。

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

看看这个:

    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow:)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

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

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

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

相关文章:

ios - 在 iOS7.1 UIButton 中设置图像不起作用?

ios - didReadRSSI 在 iOS 上调用但在 OS X 上不调用

ios - -ObjC 链接器标志导致重复符号错误

ios - IOS 最好的 QR 阅读器库是什么?

iphone - 使用 Reachability.h 时的警告

ios - 如何在 Objective C 和 swift Viewcontroller 中使用委托(delegate)

ios - 根据当前位置在 UITableview 中排序距离

iOS 6/7 Delta 无法运行 iOS 7.1

ios - SceneKit:漫反射 Alpha 与透明/透明

ios - 存储少量具有 IAP 潜力的图像