在iOS中的ViewController之间传递数据

我想在UIViewControllers之间传递一个整数值,新的iOS和有问题。

在设置值的UIViewController中,没有任何问题。 但是,值设置后, NSLog显示第二个UIViewController中的值为空。

我的应用程序将使用水平滑块来确定不同的UIViewControllerUIImage animationDuration之间的时间长度。

此方法正确接收来自水平滑块的值。 我初始化了“其他” UIViewController一个实例, imageview.somedata属于另一个视图控制器。 由于下面的NSLogs我知道值正在被正确传递。

 - (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); } 

这是该实现文件的顶部

  #import "GTSettingsVC.h" #import "GTImageView.h" #import "GTImageView.m" @interface GTSettingsVC () @end @implementation GTSettingsVC @synthesize mySlider, myTextField; @synthesize sliderValue; 

那个头文件

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

我试图发送数据到视图控制器的头文件

  #import <UIKit/UIKit.h> @interface GTImageView : UIViewController { UIScrollView* scrollView; UIPageControl* pageControl; int *someData; } @property (nonatomic) int *someData; 

我希望variablessome​​Data具有我在第一个控制器中给出的值的实现文件。 NSLog在这个实现中返回null。

  - (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]; } 

我相信你还没有掌握视图控制器生命周期的概念和理念,还有模型视图控制器(MVC)

您为第一个视图控制器( GTSettingsVC )在changeButtonPressed中定义和启动的GTImageViewGTSettingsVC的本地对象/实例。 如果您的视图控制器从GTSettingsVC过渡到GTImageView ,数据将不会传递到那里。

既然你使用的是UINavigationController ,我相信你有Segue 。 为UINavigationController传递数据最简单的方法是使用prepareForSegue 。 在过渡到这个方法之前,您将不得不准备要传递给第二个视图控制器的数据。

你也在GTImageView中犯了一些错误。 someData不是一个对象,它不应该有* 。 而且也尽量不要使用variables ,只用属性代替。 它应该是:-

  @property (nonatomic) int someData; 

GTSettingsVC.M中 ,添加以下function: –

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

GTImageView.H中

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

GTImageView.M中的 viewDidLoad

 - (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]; } 

我给Github添加了一个示例项目供您参考: https : //github.com/voyage11/PassingDataTest

没有必要采取int *。 用intreplace它,直接赋值,而不是地址。