Please help, I suffer the whole day. Suppose I have 2 ViewController (ViewController and TwoViewController). On the first view I have a button to go to the TwoViewController and UISegmentedControl. I want the array NSMutableArray under the index 0 to record the readings of this controller segment. Then transfer this array to TwoViewController and, at the click of a button on TwoViewController, change the text of lable, which is also on the same view. The Internet is full of examples, but for some reason the array comes to me empty. My code when trying to issue this through delegates

//ViewController.h #import <UIKit/UIKit.h> @protocol ProtocolPrint; @interface ViewController : UIViewController - (IBAction)go:(UIButton *)sender; - (IBAction)segment:(UISegmentedControl *)sender; @property(strong, nonatomic) NSMutableArray* info; @property(weak, nonatomic) id <ProtocolPrint> delegate; @end @protocol ProtocolPrint -(NSString*) print:(ViewController*) inf; @end // ViewController.m #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.info = [NSMutableArray arrayWithObjects:@"", nil]; } //кнопка перехода к TwoViewController - (IBAction)go:(UIButton *)sender { UIStoryboard* storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; ViewController *test = [storyBoard instantiateViewControllerWithIdentifier:@"Storyboard2"]; [self presentViewController:test animated:YES completion:nil]; } - (IBAction)segment:(UISegmentedControl *)sender { switch (sender.selectedSegmentIndex) { case 0: self.info[0] = @"значение 1"; break; case 1: self.info[0] = @"значение 2"; break; case 2: self.info[0] = @"значение 3"; break; case 3: self.info[0] = @"значение 4"; break; default: break; } } @end // TwoViewController.h #import <UIKit/UIKit.h> #import "ViewController.h" @interface TwoViewController : UIViewController <ProtocolPrint> - (IBAction)back:(UIButton *)sender; - (IBAction)editText:(UIButton *)sender; @end // TwoViewController.m #import "TwoViewController.h" @interface TwoViewController () @property (weak, nonatomic) IBOutlet UILabel *label; @property (weak, nonatomic) ViewController *oneVC; @end @implementation TwoViewController - (void)viewDidLoad { [super viewDidLoad]; _oneVC.delegate = self; } - (IBAction)back:(UIButton *)sender { [self dismissViewControllerAnimated:YES completion:nil]; } - (IBAction)editText:(UIButton *)sender { self.label.text = [self print:self.oneVC]; } - (NSString *)print:(ViewController *)inf { return inf.info[0]; } @end 

I tried to solve this issue through a singleton, through an example here.

How to transfer a variable from one class to another?

and here

http://www.imaladec.com/forum/index.php?/topic/2046-%D0% BF% D0% B5% D1% 80% D0% B5% D0% B4% D0% B0% D1% 87% D0% B0% D0% BB% D1% 8E% D0% B1% D1% 8B% D1% 85% D0% B4% D0% B0% D0% BD% D0% BD% D1% 8B% D1% 85- % D0% BC% D0% B5% D0% B6% D0% B4% D1% 83-% D0% BA% D0% BE% D0% BD% D1% 82% D1% 80% D0% BE% D0% BB% D0% BB% D0% B5% D1% 80% D0% B0% D0% BC% D0% B8 /

but the result is the same; nothing appears on the label on the label, the element of the array [0] remains @ "". Tell a newbie in iOS, what's the problem ??

  • Which table? in the second twist the array comes but is completely empty - Hudihka

1 answer 1

To begin with, delegates are not needed to pass data to the next controller. To do this, in your method - (IBAction)go:(UIButton *)sender you assign the necessary data to the created controller. Well, for such purposes, NSString sufficient:

 - (IBAction)go:(UIButton *)sender { UIStoryboard* storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; TwoViewController *test = (TwoViewController *)[storyBoard instantiateViewControllerWithIdentifier:@"Storyboard2"]; [test setSelectedValue:self.info[0]]; [self presentViewController:test animated:YES completion:nil]; } 

In the second controller:

 @property (strong, nonatomic) NSString *selectedValue; 

An example on GitHub . Try to understand the approach. Example content:

 #import "ViewController.h" #import "SecondViewController.h" @interface ViewController () @property (strong, nonatomic) NSString *currentSelection; @property (weak, nonatomic) IBOutlet UILabel *infoLabel; @property (weak, nonatomic) IBOutlet UISegmentedControl *segmentedControl; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self checkSelectedSegment:_segmentedControl]; } - (void)checkSelectedSegment:(UISegmentedControl *) segmentedControl { _currentSelection = [NSString stringWithFormat:@"Значение %ld", segmentedControl.selectedSegmentIndex]; [_infoLabel setText:_currentSelection]; } - (IBAction)onSegmetChange:(UISegmentedControl *)sender { [self checkSelectedSegment:sender]; } - (IBAction)goToNextControllerAction:(UIButton *)sender { SecondViewController *secondController = (SecondViewController *)[[self storyboard] instantiateViewControllerWithIdentifier:@"SecondViewController"]; [secondController setSelectedValue:_currentSelection]; [self presentViewController:secondController animated:YES completion:nil]; } @end @interface SecondViewController : UIViewController @property (strong, nonatomic) NSString *selectedValue; @end #import "SecondViewController.h" @interface SecondViewController () @property (weak, nonatomic) IBOutlet UILabel *infoLabel; @end @implementation SecondViewController - (IBAction)checkValueAction:(UIButton *)sender { [_infoLabel setText:_selectedValue]; } - (IBAction)closeAction:(UIButton *)sender { [self dismissViewControllerAnimated:YES completion:nil]; } @end