- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
对于基本问题,我很抱歉,我对编程还很陌生,并试图理解代码 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/
现在我正在尝试编写一个函数来在屏幕上出现键盘时向上移动框架。我开始使用 NSNNotificationCenter。我的代码正在运行但不正确。当键盘出现时,我的 formView 向上移动,但是当我开
我想知道是否有可能在应用程序处于暂停状态时从 NSNotificationCenter 运行观察者,如果选择器需要播放音乐。基本上,它在歌曲结束时运行以重置队列。是否可以激活选择器,即使应用程序处于后
应用程序可以在内部使用 NSNotificationCenter让不同的部分相互通信,但是这可以扩展到不同的应用程序可以使用它进行通信吗? 编辑 :很抱歉关于 iOS 与 OSX 的混淆。我只添加了
我正在创建一个必须在 OS X 上作为特权帮助工具运行的服务器。在其中,我尝试使用 NSSocketPort , NSFileHandle ,和NSNotificationCenter在监听套接字上执
我需要在两个不同的控制台应用程序(观察者和客户端)之间进行通信。 在观察者应用程序中,我添加了以下代码: [[NSNotificationCenter defaultCenter] postNotif
我正在使用 NSNotificationCenter 在基于文档的应用程序中发送自定义通知。 基于文档的应用程序可以有许多打开的文档。理想情况下,我希望文档及其子文档仅接收在文档或其子文档中创建的通知
NSNotificationCenter 真的很酷,我们可以用它构建非常可扩展的应用程序。我的问题是:我们是否可以在应用程序中滥用 NSNotificationCenter,即密集使用它,还是它不是一
我一直在研究 NSNotificationCenter,我一直想知道您什么时候会使用自己的自定义通知中心而不是 defaultCenter?这样做的好处是什么? 请原谅我的无知,但似乎仅使用 defa
是平衡“addObserver”的标准: [[NSNotificationCenter defaultCenter] removeObserver:self] 在 viewWillDisappear
我使用以下行添加一个观察者: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFini
当我使用一个时,内存/处理的用途是什么: [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(dismis
我从调用多个NSNotifications的所有观察器选择器到没有一个起作用。是否存在我可能已禁用的XCode / Application设置,可能会导致此行为? 这个问题几乎影响到我已经注册为观察员
基本上,我正在尝试创建一个允许用户通过 Google 或 Facebook 登录的应用程序。我已经成功创建了整个登录过程,但是在显示 Facebook 个人资料图像时遇到了一个小问题。因此,当用户选择
我的应用程序中有一个 UITableview Controller ,一个 View Controller ,我正在尝试使用 NSNotificationCenter 将 NSDictionary 从
我是 iOS 开发的新手。我正在自己学习这个。我现在对什么是通知中心有点困惑。我通过网络搜索并研究了 nsnotification 中心。 我发布这个问题只是为了澄清我对 nsnotification
在我的一个类(class)中,我发布了一条通知: [[NSNotificationCenter defaultCenter] postNotificationName:@"ALERTNOTI" obj
我有一个读取条形码的类,当我读取条形码时,我会向 NSNotificationCenter 发送通知,如下所示。 -(void)barcodeData:(NSString *)barcode type
我使用 NSNotificationcenter 来实现连接变化的事件监听器。当用户连接或断开 WiFi 网络时。当应用程序处于暂停模式时,是否可以将此通知发送到应用程序? 必须可以做到这一点,以 S
我正在使用 NSNotificationCenter,监听器使用以下代码订阅: [[NSNotificationCenter defaultCenter] addObserver:self
在我的应用程序中,我需要检测 ImagePicker 是否准备好拍照。我在这里找到了解决方案:How to know if iPhone camera is ready to take picture
我是一名优秀的程序员,十分优秀!