我有一个解决通知问题的方法很好,但我担心这可能是一个坏主意.
我有一个通知,需要由类的每个实例和类本身来处理.为了解决这个问题,我正在注册类的类和实例的通知.因为它是完全相同的通知,所以我将类和实例方法命名为相同.这遵循我们为通知处理程序的命名方式设置的标准.
这是一个坏主意吗?是否有一些我失踪的隐藏的东西.我是否会混淆未来的开发者?
+ (void)initialize
{
if (self == [SICOHTTPClient class]) {
[[self notificationCenter] addObserver:self
selector:@selector(authorizationDidChangeNotification:)
name:SICOJSONRequestOperationAuthorizationDidChangeNotification
object:nil];
}
}
- (id)initWithBaseURL:(NSURL *)url
{
self = [super initWithBaseURL:url];
if (self) {
self.parameterEncoding = AFJSONParameterEncoding;
[self registerHTTPOperationClass:[SICOJSONRequestOperation class]];
[self setDefaultHeader:@"Accept" value:@"application/json"];
if ([[self class] defaultAuthorization])
[self setDefaultHeader:@"Authorization" value:[[self class] defaultAuthorization]];
[[[self class] notificationCenter] addObserver:self
selector:@selector(authorizationDidChangeNotification:)
name:SICOJSONRequestOperationAuthorizationDidChangeNotification
object:nil];
}
return self;
}
- (void)dealloc
{
[[[self class] notificationCenter] removeObserver:self
name:SICOJSONRequestOperationAuthorizationDidChangeNotification
object:nil];
}
#pragma mark Notifications
- (void)authorizationDidChangeNotification:(NSNotification *)notification
{
NSString *authorization = …Run Code Online (Sandbox Code Playgroud) 我看到了一些关于在同一个类中添加观察者和句柄的例子,但我想知道的是,是否可以在第一个视图控制器中添加观察者并在第二个视图控制器中处理它?
我希望不断从第一个视图控制器发送距离并在第二个控制器中处理它.第二个视图控制器添加为子视图:addSubview,addChildViewController.
它就像android中的广播一样.
objective-c observers nsnotification nsnotificationcenter ios
我在我的应用程序中实现了NSNotificationCenter.我在完成图像解码时发送通知.第一次图像解码将完成8次.所以通知假设发送8次.但它调用64次(8*8).
这是我的代码我是如何实现的 - > //初始化
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(getHRImage:)
name:@"DecodeComplete" object:nil];}
Run Code Online (Sandbox Code Playgroud)
//调用方法
-(void)getHRImage:(NSNotification *) notification
{
if ([[notification name] isEqualToString:@"DecodeComplete"])
NSLog (@"Successfully received the DecodeComplete notification! ");
}`
Run Code Online (Sandbox Code Playgroud)
//解除分配
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"DecodeComplete" object:self];
//[super dealloc];
}
Run Code Online (Sandbox Code Playgroud)
//发布通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"DecodeComplete" object:self];
Run Code Online (Sandbox Code Playgroud)
有人可以建议我做错了.
提前致谢.
//调用方法是这样的(调用8次)
-(void)decode
{
NSLog(@"---------- Decoding is Complete ---------");
[[NSNotificationCenter defaultCenter] postNotificationName:@"MdjDecodeComplete" object:self];
}
Run Code Online (Sandbox Code Playgroud) 在我的iPad应用程序中,在一个类中我注册了一个通知:
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(selectedList:) name:@"TTSelectedList" object:nil];
Run Code Online (Sandbox Code Playgroud)
我的selectedList:方法看起来像这样:
- (void)selectedList:(NSNotification*)notification
{
NSLog(@"received notification");
}
Run Code Online (Sandbox Code Playgroud)
然后在另一个类(a UITableViewController)中,当选择一行时,我发布该通知:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"posting notification");
[[NSNotificationCenter defaultCenter] postNotificationName:@"TTSelectedList" object:nil];
}
Run Code Online (Sandbox Code Playgroud)
我可以确认通知正在发布,因为"发布通知"被记录到控制台,但是从未调用"已接收通知",这意味着未收到通知且未调用选择器.我无法弄清楚造成这种情况的原因.
谢谢
cocoa-touch objective-c nsnotification nsnotificationcenter ipad
我对NSNotification对象有一种奇怪的行为.
我的应用程序有一个导航控制器,第一个视图是一个表视图,第二个视图只是一个显示所选单元格数据的视图控制器.
所以在这个数据视图控制器中,当我按下按钮时,我发送通知.该通知最初也有效.
但是当我回到表视图,并再次推栈中的数据视图控制器,并通知触摸按钮,整个应用程序没有错误日志崩溃.
Xcode只突出显示这一行:
[[NSNotificationCenter defaultCenter]
postNotificationName:@"toggleNoteView" object:nil];
Run Code Online (Sandbox Code Playgroud)
我发送通知的功能:
- (IBAction) toggleNoteView: (id) sender
{
[[NSNotificationCenter defaultCenter]
postNotificationName:@"toggleNoteView" object:nil];
}
Run Code Online (Sandbox Code Playgroud)
这是接收者:
- (id)init {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(toggleNoteView:)
name:@"toggleNoteView" object:nil];
...
}
- (void) toggleNoteView:(NSNotification *)notif {
takingNotes = !takingNotes;
}
Run Code Online (Sandbox Code Playgroud)
编辑:现在我确实得到了一些错误日志.
2011-06-27 23:05:05.957 L3T[3228:707] -[UINavigationItemView toggleNoteView:]: unrecognized selector sent to instance 0x4b235f0
2011-06-27 23:05:06.075 L3T[3228:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationItemView toggleNoteView:]: unrecognized selector sent to instance 0x4b235f0'
*** Call stack at first throw: …Run Code Online (Sandbox Code Playgroud) 视频播放完毕后,我正在显示文本.我正在使用通知技术来实现这一目标.唯一的问题是Observer每隔一段时间被调用两次.它触发"itemDidFinishPlaying"两次(因此同名的方法).我无法预测何时.我不知道为什么.它看起来是随机的(我知道这听起来很奇怪)就好像它工作得很好让我们说连续15次,然后下一次这种行为突然发生了.我做了一个重建并运行应用程序,这次它连续运行19次,然后两次调用Observer等等......不可预测.我已经尝试过每个场景来预测bug以便修复它.到目前为止,这是不可能的.所以我有2个问题.
1)为什么会发生并"随机"?
2)如何解决这个双重调用问题?
这两个以下的对话也没有帮助:
为什么NSNotification中的Observer调用了两次....?
如何阻止NSNotification中的Observer调用两次?
请在下面找到我的代码:
- (void) playAnimation: (NSString *) theString {
UIView *thisCurrentView = self.currentView;
UIView *thisReplacementView = [[UIView alloc] init];
//[avPlayer pause];
[self replaceView: thisCurrentView withView: thisReplacementView];
NSString *filepath = [[NSBundle mainBundle] pathForResource:theString ofType:@"mov"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
// First create an AVPlayerItem
AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:fileURL];
// Subscribe to the AVPlayerItem's DidPlayToEndTime notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
// Pass the AVPlayerItem to a new player
controlledPlayer = [[AVPlayer alloc] initWithPlayerItem:playerItem];
AVPlayerLayer …Run Code Online (Sandbox Code Playgroud) 有什么区别NSNotification的object和userInfo?
当我发布带有参数的通知时,我可以使用object或userInfos执行它。但我不知道这两种方式有什么区别。
使用有userInfo什么好处吗?还是用object够了?
可以先使用-removeObserver:,然后-addObserver:使用相同的名称调用吗?或者-addObserver:之前有先行规则-removeObserver:?
我尝试使用OS 4.0,它似乎没问题(没有崩溃,警告等等).
-(void) setObserver
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:OBSERVER_NAME object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector: @selector(selectorName)
name:OBSERVER_NAME
object:nil];
}
Run Code Online (Sandbox Code Playgroud)
原因是为了防止两个具有相同selectorName方法的观察者被调用两次,假设该-setObserver方法在其内部-viewDidLoad和内存警告被发出时再次被调用.
另外,我需要在调用-removeObserver:期间调用-dealloc吗?
在我的UITableViewCell中,我有一个方法initNotification,它由cellForRowAtIndexPath中的TableViewController调用,其中创建了TableCells.
我的问题是,每次重新加载此视图时,都会再次调用initNotification方法,因此当出现Notification时,NotificationHandle将被调用x次!
我在尝试删除Observer之前再次添加它:
-(void) initNotification{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(handleNotificationOnOff:)
name:[[NSString alloc] initWithFormat:@"%@",[self.light beckhoffOnOff]]
object:nil];
}
Run Code Online (Sandbox Code Playgroud)
但这也不起作用.问题是,我不能使用bool-flag或类似的东西,因为ViewController总是重新初始化Cells.
是否有正确的方法从NotificationCenter中删除NotificationHandle?
编辑:这是我创建自定义TableViewCells的方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
Light* l = [[staticModel.model getRoomAtIndex:[indexPath section]]getLightAtIndex:[indexPath item]];
if([l typ]==ONOFF){
TableCellLight *conof = [tableView dequeueReusableCellWithIdentifier:@"ReuseIDOnOff" forIndexPath:indexPath];
LightOnOff *lonof = (LightOnOff*) l;
[[conof label] setText: [lonof bezeichnung]];
conof.light=lonof;
[conof initNotification];
cell = conof;
}
if([l typ]==DIMMER){
TableCellLightDim *cdim = [tableView dequeueReusableCellWithIdentifier:@"ReuseIDDim" forIndexPath:indexPath];
LightDim *ldim= (LightDim*) l;
[[cdim label] setText: [ldim bezeichnung]];
[[cdim …Run Code Online (Sandbox Code Playgroud) 我想检查我的视图是否在监听UIApplicationWillResignActiveNotification。如果它正在监听,那么我想在dealloc期间将其删除。现在我想知道是否有使用目标c做到这一点的方法?
我不尝试避免为通知添加多个内容。这是我要做什么的更多解释。
我有自定义的gridView。我可以通过启用缩放或禁用缩放来初始化它。如果启用了缩放的init,我将自己添加为UIApplicationWillResignActiveNotification的观察者,但是如果禁用了缩放的init,则它不会将自身添加为该通知的观察者。现在,在dealloc中,我想删除该gridView作为该通知的观察者。所以我想知道是否有办法找出gridView是否正在侦听该通知。
objective-c nsnotifications nsnotification nsnotificationcenter ios
nsnotification ×10
ios ×6
objective-c ×6
cocoa-touch ×3
ipad ×2
iphone ×2
avplayer ×1
coding-style ×1
observers ×1
uitableview ×1
uitextview ×1