Pergunta

I have an application that I am working on that has several viewControllers, with each one displaying a single test for the user to do. At the moment, the user is able to go through each one individually. However, I am trying to implement a wizard-like function where a user can select multiple tests, or all tests, and the application in turn will iterate through each test, presenting each screen to the user, and once the user has submitted input, the application will move to the next test sequentially. Once all the tests have completed, the user will be brought back to the main screen. From what I have read, NSNotifications would be the best way to do this, but I honestly am new to this and need help. I realize that I should have in the method that initiates the wizard the line:

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(testChange:)
                                                 name:@"Test"
                                               object:nil];

I also know that once each viewController has finished running, it is to post a notification in the following way:

[[NSNotificationCenter defaultCenter] postNotificationName:@"Test" object:self];

My question is, if I have ten or twenty viewControllers selected by the user from a table, and these selections are stored in an array, do I need to have as many calls to the addObserver method, and as many postNotifications? What I would like to do is simply go through each viewController (as selected by the user), and once the user has finished submitted input to that viewController, that viewController should send a message, and the user should move on to the next viewController, and after finishing all tests, return to the main screen. Just as an FYI, I need to call each ViewController's (viewDidLoad) method.

I apologize if my question is confusing.

Foi útil?

Solução

You are right, you need to call addObserver for all the view controllers you have, if you want all of them to receive the notifications. I created a small code piece to show you how it is done. I think you have all the answers you are asking for. Just try it out.

#import "ViewController.h"

@interface Employee:NSObject
@end

@implementation Employee
-(void)testMethod2:(NSNotification *)not{
    NSLog(@"Test method2 is called");
}
@end

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)postNotifications:(id)sender {
    Employee *employee = [[Employee alloc] init];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(testMethod:) name:@"Notification" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(testMethod1:) name:@"Notification" object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:employee selector:@selector(testMethod2:) name:@"Notification" object:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Notification" object:nil];
}

-(void)testMethod:(NSNotification *)not{
    NSLog(@"Test method is called");
}
-(void)testMethod1:(NSNotification *)not{
    NSLog(@"Test method1 is called");
}
@end
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top