0

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.

syedfa
  • 2,801
  • 1
  • 41
  • 74
  • This is not a direct answer to your question so I am posting as a comment. I would strongly discourage the use of notifications here in favor of delegation. Create a generic delegate protocol to which your parent table view controller conforms and assign it as the delegate of each child view controller. In this way, each child can inform it's delegate (the table view controller) when it is done, at which point you can dismiss it and present the next child view controller. – nickbona Jan 09 '13 at 18:54

1 Answers1

0

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
Srikanth
  • 1,725
  • 2
  • 10
  • 11
  • Thanks so much for posting this example, as it really has helped tremendously! I do have one follow up question for you, and that is, despite the number of "addObserver" methods that I need, from your example, it appears that I only need one "postNotificationName" method. Is this correct? – syedfa Jan 09 '13 at 20:52
  • Yes. I think you should just try it out. There are ofcourse many ways to solve the same problem, but once you give it a try, you will figure it out. – Srikanth Jan 09 '13 at 21:36