我现在正在学习通知编程,有一个非常简单的项目有两个类有一点问题,在发布通知时不调用通知选择器方法。这很奇怪,希望有人帮我找出问题所在,我非常感谢!
我的源代码:
视图控制器.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *pushButton;
- (IBAction)presentViewController:(id)sender;
@end
视图控制器.m
#import "ViewController.h"
#import "ViewController2.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (IBAction)presentViewController:(id)sender
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"networkNotification"
object:self];
ViewController2 *viewController2 = [self.storyboard instantiateViewControllerWithIdentifier:@"viewController2"];
[self presentViewController:viewController2
animated:YES
completion:nil];
}
@end
ViewController2.h
#import <UIKit/UIKit.h>
@class ViewController;
@interface ViewController2 : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *testLabel;
@property (nonatomic, strong) ViewController *viewController;
@end
视图控制器2.m
#import "ViewController2.h"
#import "ViewController.h"
@interface ViewController2 ()
@end
@implementation ViewController2
@synthesize testLabel;
- (void)viewDidLoad
{
[super viewDidLoad];
self.viewController = [[ViewController alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(recievingNotifications:)
name:@"networkNotification"
object:self.viewController];
}
- (void)recievingNotifications:(NSNotification *)aNotification
{
if ([[aNotification name] isEqualToString:@"networkNotification"])
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
self.testLabel.text = @"Good";
}
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:@"networkNotification"
object:self.viewController];
}
@end