在我使用故事板的應用程序中,我在mainViewController中使用MapView。在mainViewController的頂部,我使用UITableViewController從屏幕底部顯示UIContainerView
。當用戶點擊行時,我將通知發送到mainViewController,以便通過動畫更改地圖區域。當用戶選擇特定的tableViewCell時以及用戶點擊位於navigationViewController中的按鈕時,也會發送類似的通知。除了在DONE按鈕方法中調用的方法之外,一切正常。多個NSNotificationCenter問題
主視圖控制器 - (無效)viewDidLoad方法...
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideContainerView:)
name:HideContainerView object:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeRegionForUser:)
name:ChangeRegionForUser
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideContainerView:)
name:ChangeRegionToInitial
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeRegionToFitWearerAnnotations:)
name:ChangeRegionToFitWearerAnnotations
object:nil];
主視圖控制器觀察員方法:
#pragma mark NSNotificationCenter methods
-(void)changeRegionForUser:(NSNotification*)notification
{
NSLog(@"%@",notification.description);
[self zoomToFitUserLocationWithLatitude:-33.861858 longitude:151.210546 andUserInfo:nil];
}
-(void)hideContainerView:(NSNotification*)notification
{
NSLog(@"%@",notification.description);
[self zoomToFitMapAnnotations:_mainMapView];
[UIView beginAnimations:@"HideContainerAnimation" context:nil];
[UIView setAnimationDuration:0.5];
[_containerView setFrame:CGRectMake(0, 600, 320, 284)];
[UIView commitAnimations];
}
-(void)changeRegionToFitWearerAnnotations:(NSNotification*)notification
{
NSLog(@"%@",notification.description);
[self zoomAnnotationsOnMapView:_mainMapView toFrame:CGRectMake(0, 0, 320, 200) animated:YES];
}
表視圖控制器( DID-SELECT-ROW-AT-INDEX-PATH方法)/ /它正常工作
[[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeRegionForUser" object:self];
表視圖控制器(DONE按鍵法)//這是行不通的
-(void)doneButtonAction:(id)sender
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"HideContainerView" object:self];
}
我的故事板
主VIEW CONTROLLER.H
@interface MainScreenViewController : UIViewController<MKMapViewDelegate>
extern NSString * const HideContainerView;
extern NSString * const ChangeRegionForUser;
extern NSString * const ChangeRegionToInitial;
extern NSString * const ChangeRegionToFitWearerAnnotations;
主VIEW Controller.m或者
#import "MainScreenViewController.h"
NSString* const HideContainerView = @"HideContainerView";
NSString* const ChangeRegionForUser = @"ChangeRegionForUser";
NSString* const ChangeRegionToInitial = @"ChangeRegionToInitial";
NSString* const ChangeRegionToFitWearerAnnotations [email protected]"ChangeRegionToFitWearerAnnotations";
你可以發佈你的項目?任何使用NSNotificationCenter的最佳實踐都是viewWillAppear上的addOserver和ViewController上的viewWillDisappear上的removeObserver。 –
當然,我知道,但TableViewController顯示在UIContainer中,並始終處於活動狀態。我不確定viewWillDisappear方法是否會改變我的情況。不幸的是我不能發佈我的項目,但我可以上傳一些方法,如果你想... – sonoDamiano
在這一行([[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideContainerView :) name:HideContainerView object:self];)是HideContainerView字符串常量?你在哪裏定義了它? –