我正在尝试在UIViewControllers
,iOS新手之间传递一个整数值并遇到问题。
在UIViewController
设置值的位置,没有问题。但是在设置NSLog
了值之后,第二个值显示为null UIViewController
。
我的应用将使用水平滑块确定UIImage animationDuration
不同之间的时间长度UIViewController
。
此方法从水平滑块正确接收值。我初始化“其他”的一个实例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;
我希望变量someData具有在第一个控制器中赋予它的值的实现文件。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];
}
我相信您尚未掌握View Controller生命周期以及Model View Controller(MVC)的概念/思想。
该GTImageView您定义和启动changeButtonPressed你的第一个视图控制器(GTSettingsVC)是为本地对象/实例GTSettingsVC。如果您的视图控制器从GTSettingsVC过渡到GTImageView,那么数据将不会传递到那里。
既然您正在使用UINavigationController
,我相信您已经拥有了Segue
。传递数据的最简单方法UINavigationController
是使用prepareForSegue
。在过渡到第二视图控制器之前,您必须准备要传递给第二视图控制器的数据。
您还会在GTImageView中犯一些错误。someData不是对象,它不应该带有*。并且也请尽量不要使用variable,而应仅使用属性。它应该是:-
@property (nonatomic) int someData;
在GTSettingsVC.M中,添加以下功能:
-(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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句