1

私は現在通知プログラミングを学んでいます。非常に単純なプロジェクトに、通知を投稿するときに通知セレクターメソッドを呼び出さないという小さな問題がある2つのクラスがあります。それは非常に奇妙です。誰かが問題が発生した場所を見つけるのを手伝ってくれることを願っています。

私のソースコード:

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
4

2 に答える 2