Теперь я изучаю программирование уведомлений, у вас очень простой проект, есть два класса, которые имеют небольшие проблемы, которые не вызывают метод выбора уведомлений при отправке уведомления. Это очень странно, надеюсь кто-то поможет мне найти, где возникли проблемы, я очень благодарен за это!iOS NSNotification Простые проблемы с проектом Xcode
Мой исходный код:
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *pushButton;
- (IBAction)presentViewController:(id)sender;
@end
ViewController.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
ViewController2.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
В чем проблема? – Undo
Почему вы передаете себя как объект, кажется, что nil будет отлично работать как с отправителем, так и с приемником. – JeffN
Вы уверены, что 'presentViewController' даже вызван? –