I try to pass data from one ViewController to secondControler however seem it not work. I use NSNotification. - 2 Controller have same class "ViewController"

In viewcontroller.m

- (void)viewDidLoad {
       [super viewDidLoad];
       [[NSNotificationCenter defaultCenter] addObserver:self 
        selector:@selector(ProcessBarLoading) name:@"buttonPressed" object:nil];
   }
-(void)ProcessBarLoading{
      _labelTest.stringValue = @"TESTING";
    }
- (IBAction)test:(id)sender {
     [[NSNotificationCenter defaultCenter] postNotificationName:@"buttonPressed" object:self];
      NSStoryboard *storyboard = [NSStoryboard storyboardWithName:@"Main" bundle: nil];
      NSViewController * vc = [storyboard instantiateControllerWithIdentifier:@"SheetViewController"];    
[self presentViewControllerAsSheet:vc];
      }

When run program and press button, there're no update Label Text at all. Do you know why and how I can fix. enter image description here

When [press and show view2 there're no update value in Label field

New Code: In SecondViewController.m

@interface SencondViewController ()

@end

@implementation SencondViewController
@synthesize progressValue;
@synthesize labelView;
- (void)viewDidLoad {
 [super viewDidLoad];
 // Do view setup here.
 labelView.stringValue =progressValue;
 }

In FirstViewCOntroller:

- (IBAction)test:(id)sender {
     self->uide = @"0";
     [self performSegueWithIdentifier:@"showRecipeDetail" sender:self->uide];
NSStoryboard *storyboard = [NSStoryboard storyboardWithName:@"Main" bundle: nil];

NSViewController * vc = [storyboard instantiateControllerWithIdentifier:@"SheetViewController"];
[self presentViewControllerAsSheet:vc];

- (void)prepareForSegue:(NSStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showRecipeDetail"]) {
SencondViewController * secondVC = segue.destinationController;
secondVC.progressValue = uide;
}  
}
- (IBAction)test2:(id)sender {

uide = @"80";
[self performSegueWithIdentifier:@"showRecipeDetail" sender:uide];
[self.view displayIfNeeded];
}

So whether I press Button 1(test) other Button2 (test2) alway show new view with update value. What I need is only show 1 view.

2

There are 2 best solutions below

3
Fabio On

Why do you need use a nsnotification the easy way is use a prepareForSegue or Delegation

This is an examample

    #pragma mark - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
        if ([segue.identifier isEqualToString:@"myId"]) {
            SecondViewController *vc = segue.destinationViewController;
            vc.myDataToPass = self.myValueInMyFirstViewController;
        }
}
6
Deep Arora On

Notification pattern is not recommended for doing this. Use notification when you want to pass some data to multiple objects on some event.

To solve this problem:

Step 1: You should change your View Controller names to FirstViewController and SecondViewController, and have a property declared in your SecondViewController whose value you want to set from the FirstViewController.

Step 2: Finally, in the prepare for Segue method of the FirstViewController, set the data.

In Objective-C, you can try this code:

#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()

@end

@implementation FirstViewController

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

//This will trigger the prepareForSegue method

-(IBAction) someButtonClick {
    [self performSegueWithIdentifier:@"YourSequeId" sender:nil];
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    SecondViewController * secondVC = segue.destinationViewController;
    secondVC.someValue = @"PassYourValueHere";

}



@end

and in the header file of the SecondViewController, declare the property:

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

@property (nonatomic,strong) NSString *someValue;

@end

In the implementation file of the SecondViewController, write:

#import "SecondViewController.h"

@interface SecondViewController ()

@property (nonatomic,weak) IBOutlet UITextField *yourTextField;
@end

@implementation SecondViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    self.yourTextField.text = self.someValue

    // Do any additional setup after loading the view.
}



@end