简体   繁体   中英

Passing data between ViewControllers in iOS

I am trying to pass an integer value between UIViewControllers , new to iOS and having problems.

In the UIViewController where the value is being set, there are no problems. But after the value is set NSLog is showing that the value is null in the second UIViewController .

My app is going to use the horizontal slider to determine the length of time in between a UIImage animationDuration in a different UIViewController .

This method is correctly receiving the value from the horizontal slider. I initialized an instance of the "other" UIViewController , imageview.somedata belongs to the other view controller. I know the value is being passed correctly because of the NSLogs below.

- (IBAction) changeButtonPressed:(id)sender {
imageView = [[GTImageView alloc] initWithNibName:@"GTImageView" bundle:nil];
NSLog(@"text value = %@", myTextField);
NSString *textValue = [myTextField text];
int value = [textValue floatValue];
if (value < 0) value = 0;
if (value > 100) value = 100;
mySlider.value = value;
sliderValue = &value;
NSLog(@"sliderValue = %d", *(sliderValue));
myTextField.text = [NSString stringWithFormat:@"%.1d", value];
if ([myTextField canResignFirstResponder]) [myTextField resignFirstResponder];
imageView.someData = *(sliderValue);
NSLog(@"imageView.someData = %d", imageView.someData);
}

This is the top of that implementation file

    #import "GTSettingsVC.h"
    #import "GTImageView.h"
    #import "GTImageView.m"
    @interface GTSettingsVC ()
    @end

    @implementation GTSettingsVC
    @synthesize mySlider, myTextField;
    @synthesize sliderValue;

That header file

    #import "GTImageView.h"
    @interface GTSettingsVC : UIViewController
    {
    IBOutlet UISlider *mySlider;
    IBOutlet UITextField *myTextField;
    GTImageView *imageView;
    int *sliderValue;
    }

    @property (nonatomic) int *sliderValue;

The header file of the view controller I am trying to send the data to

    #import <UIKit/UIKit.h>

    @interface GTImageView : UIViewController
    {
    UIScrollView* scrollView;
     UIPageControl* pageControl;
     int *someData;
     }
     @property (nonatomic) int *someData;

The implementation file where I want the variable someData to have the value I gave it in the first controller. NSLog is returning null in this implementation.

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



      UIImageView *animatedImageView = 
     [[UIImageView alloc] i nitWithFrame:CGRectMake(0,      55, 400, 550)];
      [self.view addSubview:animatedImageView];

       animatedImageView.animationImages = [NSArray arrayWithObjects:
                                     [UIImage imageNamed:@"IMG_0052.JPG"],
                                     [UIImage imageNamed:@"IMG_0054.JPG"],
                                     [UIImage imageNamed:@"IMG_0081.JPG"],
                                     nil];
      NSLog(@"some data = %@", someData);
   //   NSLog is returning null
      int x = someData;
      animatedImageView.animationDuration = 
      x * [animatedImageView.animationImages count];
      [animatedImageView startAnimating];
      [self.view addSubview: animatedImageView];

      }

There is no need to take int *. Replace it by int and assign the value directly rather than address.

I believe you have yet to grasp the concept/idea for View Controller Life Cycle and also Model View Controller (MVC) .

The GTImageView that you defined and initiated in changeButtonPressed for your First View Controller ( GTSettingsVC ) is a local object/instance for GTSettingsVC . If your view controller transition from GTSettingsVC to GTImageView , the data will not pass to there.

Since you are using UINavigationController , I believe you have Segue . The easiest way to pass data for UINavigationController is using prepareForSegue . You will have to prepare the data that you want to pass to the second view controller in this method before the transition to it.

You also make some mistake in GTImageView . someData is not an object, it should not have * . And also try not to use variable , use just the property in stead. It should be:-

 @property (nonatomic) int someData;

In GTSettingsVC.M , add the following function:-

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
  GTImageView * viewController = segue.destinationViewController;
  viewController.someData = 99;
}

In GTImageView.H

@interface GTImageView : UIViewController
{
    UIScrollView* scrollView;
    UIPageControl* pageControl;
}
@property (nonatomic) int someData;
@end

In viewDidLoad in GTImageView.M

- (void)viewDidLoad
{
  [super viewDidLoad];
  UIImageView *animatedImageView =
  [[UIImageView alloc] initWithFrame:CGRectMake(0,55, 400, 550)];
  [self.view addSubview:animatedImageView];

  animatedImageView.animationImages = [NSArray arrayWithObjects:
                                     [UIImage imageNamed:@"IMG_0052.JPG"],
                                     [UIImage imageNamed:@"IMG_0054.JPG"],
                                     [UIImage imageNamed:@"IMG_0081.JPG"],
                                     nil];
  NSLog(@"some data = %d", self.someData);
  int x = self.someData;
  animatedImageView.animationDuration =
  x * [animatedImageView.animationImages count];
  [animatedImageView startAnimating];
  [self.view addSubview: animatedImageView];
}

I added a sample project to Github for your reference:- https://github.com/voyage11/PassingDataTest

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM