0

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.

Jacky Le
  • 51
  • 9
  • Possible duplicate of [Passing Data between View Controllers](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – il3v Aug 23 '18 at 10:07

2 Answers2

1

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;
        }
}
Fabio
  • 1,913
  • 5
  • 29
  • 53
  • How to call this method, could you make clear when I want to set a text filed from Viewcontroler 1 – Jacky Le Aug 23 '18 at 02:37
  • In the button click of action, you can call performSegue method. You need to pass an Segue ID in in. In the story, you make a sequel from FirstViewController to SecondViewController and give an ID to this Segue. – Deep Arora Aug 23 '18 at 06:15
  • I update the the code in the question. 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. – Jacky Le Aug 23 '18 at 07:07
1

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
Deep Arora
  • 1,900
  • 2
  • 24
  • 40
  • I use this method then it work. However each time I pass data, Second view alway create the new View. If I select single presentation from Secondcontroller then new value didn't update on the view. How can I fix it. – Jacky Le Aug 23 '18 at 06:07
  • Please share GitHub repo of your code, I will check. SecondViewController when created is a new object and it will have it's own View. You need to create an Outlet of your TextField in the SecondViewController and in the viewDidLoad of SecondViewController, you can set the text of the TextField. – Deep Arora Aug 23 '18 at 06:19
  • I update the the code in the question. 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. – Jacky Le Aug 23 '18 at 07:09
  • And one more thing how I can close SencondView window programing. – Jacky Le Aug 23 '18 at 08:05
  • I update the code into google driver. could you help to check. What I need is, when press button process run-> 2nd View showed and update progress value(there're 2 scode, I keep 2 kind of Seque, 1 is Show, other one is sheet both not work as I expected) --> Finnish process 2nd View Auto Close (This one I still don't know how to make it happend) https://drive.google.com/open?id=1ho8OI_ftXMWmhGFrqDmGFguN8Oy-TNZI. https://drive.google.com/open?id=1NCDQ1nzb6modBJLDu_X1wY-RhRrZbFSH – Jacky Le Aug 23 '18 at 16:01
  • Deep Arora could you help – Jacky Le Aug 24 '18 at 05:16