ios - NSNotificationCenter 是 UITextView 的一部分吗?
<p><p>对于这个基本问题,我很抱歉,我对编程很陌生,并试图理解代码苹果建议器中的某些内容,以便为我想要执行的某些内容提供某种解决方案。</p>
<p>我创建了一个简单的笔记应用程序,非常基本,目前我有:
1. CreateNoteViewController
2. NotesListTableViewController </p>
<p>所以我想在创建笔记并且用户在键盘下方键入时添加一种行为,以便调整文本大小,以便用户仍然可以看到他键入的内容并且文本不会在键盘后面。</p>
<p>所以我添加了一些苹果建议的代码行来完成。</p>
<p>在 <code>viewWillAppear</code> 中调用了 NSNotificationCenter 上的一个方法,我无法理解在哪里声明了 NSNotificationCenter 对象...?</p>
<p>这是我的 CreateNoteViewController (请帮助我理解为什么他们可以执行此调用):</p>
<pre><code>#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
{
;
// listen for keyboard hide/show notifications so we can properly adjust the table's height
[ addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[ 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 = [ CGRectValue];
NSTimeInterval animationDuration =
[ 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;
;
;
self.textField.frame = frame;
;
}
- (void)keyboardWillShow:(NSNotification *)aNotification
{
];
}
- (void)keyboardWillHide:(NSNotification *)aNotification
{
];
}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if (sender != self.createButton) return;
if (self.textField.text.length > 0) {
self.note = [ init];
self.note.content = self.textField.text;
}
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = ;
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
;
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
;
// Dispose of any resources that can be recreated.
}
@end
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><blockquote>
<p>Is NSNotificationCenter is part of UITextView?</p>
</blockquote>
<p>不,不是。 NSNotificationCenter 顾名思义是一个通知中心。对象可以使用 NSNotificationCenter 订阅通知和发布通知,以处理和通知某些事件。</p>
<p>他们使用 NSNotificationCenter 让 viewcontroller 订阅 UIKeyboardWillHideNotification 和 UIKeyboardWillShowNotification 事件。</p>
<p>看看这个:</p>
<pre><code> [ addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
</code></pre>
<p>NSNotificationCenter 被设计为用作单例(我相信这是正确的术语,如果我错了,请纠正我)所以我们通过调用类方法 defaultCenter 来访问这个应用程序进程的 NSNotificationCenter。它添加了观察者'self'(在这种情况下是 ViewController 的一个实例),并且基本上指示它在触发 UIKeyboardWillShowNotification 名称下的通知时向观察者发送消息keyboardWillShow。</p>
<p>什么对象会触发 UIKeyboardWillShowNotification?好吧,它不是 UITextView,这个通知名称实际上是在 UIWindow.h 中定义的,所以它可能来自那里,而它又可能是从 UIKeyboard 调用的,据我所知,它不是公共(public) API。</p></p>
<p style="font-size: 20px;">关于ios - NSNotificationCenter 是 UITextView 的一部分吗?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/22825126/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/22825126/
</a>
</p>
页:
[1]