How to make data transfer between ViewController , i.e. from one controller to another?
I want to make data entry (settings) on one controller, and output this data on the other.
If you can, tell me an example, please!

 ViewController.h #import <UIKit/UIKit.h> #import "ViewController2.h" @interface ViewController : UIViewController @property (weak, nonatomic) IBOutlet UITextField *log; - (IBAction)vper:(id)sender; @end ViewController.m #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void) someButtonTapped:(id)sender { ViewController2 *classB = [[ViewController2 alloc] init]; //создаете экземпляр класса classB.textFieldValue = _log.text; // присваеваете значение переменной } - (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)vper:(id)sender { } @end ViewController2.h #import <UIKit/UIKit.h> #import "ViewController.h" @interface ViewController2 : UIViewController @property (nonatomic, strong) NSString *textFieldValue; @property (weak, nonatomic) IBOutlet UILabel *lab; - (IBAction)naz:(id)sender; @end ViewController2.m #import "ViewController2.h" @interface ViewController2 () @end @implementation ViewController2 - (void)viewDidLoad { NSLog(@"textFieldValue = %@", _textFieldValue); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)naz:(id)sender { } @end 
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

2 answers 2

Suppose you have two classes, class A and class B Class А - data entry and class В - data processing. In the method in which we process the input data, you need to create an instance of class В

 - (void) someButtonTapped:(id)sender { MyClassB *classB = [[MyClassB alloc] init]; //создаете экземпляр класса classB.textFieldValue = _textField.text; // присваеваете значение переменной } 

In your class B in the .h file, add the property

 @property (nonatomic, strong) NSString *textFieldValue; 

Now the variable textFieldValue contains the value that you passed to it in class А

For example, in the viewDidLoad method (class B) you can check:

 - (void)viewDidLoad { NSLog(@"textFieldValue = %@", _textFieldValue); } 

NB! Instance of the class, you need to create only once, if during the transition to the new view you make another instance, then the value of _textFieldValue will be nil .

UPDATE

Since you are creating a link to a new controller in IB , you need to configure segue . To do this in IB , highlight the connection between the first and second controller, in the Utilities tab, select Attributes and specify Identifier - for example, segueNext .

enter image description here

Further, in order to transfer data, you need a method

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

In which you specify the necessary connection, and transfer data to the controller. Here is an example:

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString:@"segueNext"]) { ViewController2 *classB = [segue destinationViewController]; classB.textFieldValue = _log.text; } } 

Second option

Remove from the IB connection and make the transitions in the code. For example:

 - (IBAction)vper:(id)sender { ViewController2 *classB = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController2"]; classB.textFieldValue = _log.text; [self presentViewController:classB animated:YES completion:nil]; } 
  • Thanks for the answer! Only he swears at _textField.text in (void) someButtonTapped (use of undeclared identifier '_textfield'), what to do? - Rad
  • _textField - replace it with the name of your text field. I wrote _textField for example. Or first, just classB.textFieldValue = @"Любой текст" text, for example classB.textFieldValue = @"Любой текст" ; - Vitali Eller
  • Did as you wrote. Does not work. - Rad
  • How to throw the code? - Rad
  • Add code to the question or link to the resource - Vitali Eller

In one controller, you can save data in UserDefaults (or a similar data manager) and read it in another controller in the viewWillAppear method. So the data will be saved even between restarts of the application.

  • But for this you just have to tear off your hands. - Max Mikheyenko
  • I agree, one must think of such a thing - Igor