gpt4 book ai didi

ios - NSNotificationCenter 是 UITextView 的一部分吗?

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

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

我创建了一个简单的笔记应用程序,非常基础,目前我有: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)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 调用的,这不是一个公共(public) API。

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

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