对于这个基本问题,我很抱歉,我对编程很陌生,并试图理解代码苹果建议器中的某些内容,以便为我想要执行的某些内容提供某种解决方案。
我创建了一个简单的笔记应用程序,非常基本,目前我有:
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-推荐答案 strong>
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/
|