gpt4 book ai didi

ios - 在 iOS 中的 ViewController 之间传递数据

转载 作者:行者123 更新时间:2023-11-29 10:42:36 25 4
gpt4 key购买 nike

我正在尝试在 UIViewControllers 之间传递一个整数值,这对 iOS 来说是新的并且有问题。

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

我的应用程序将使用水平 slider 来确定不同 UIViewController 中的 UIImage animationDuration 之间的时间长度。

此方法正确地从水平 slider 接收值。我初始化了一个“other”的实例UIViewControllerimageview.somedata属于other view controller。由于下面的 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;

我试图将数据发送到的 View Controller 的头文件

    #import <UIKit/UIKit.h>

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

我希望变量 someData 具有我在第一个 Controller 中赋予它的值的实现文件。 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 Life CycleModel View Controller (MVC) 的概念/想法。

您在 changeButtonPressed 中为您的第一个 View Controller (GTSettingsVC) 定义和启动的 GTImageView 的本地对象/实例>GTSettingsVC。如果您的 View Controller 从 GTSettingsVC 转换到 GTImageView,数据将不会传递到那里。

由于您正在使用 UINavigationController,我相信您有 Segue。为 UINavigationController 传递数据的最简单方法是使用 prepareForSegue。在转换到第二个 View Controller 之前,您必须准备好要传递给此方法中的第二个 View Controller 的数据。

您在 GTImageView 中也犯了一些错误。 someData 不是对象,它不应该有*。并且尝试不使用变量,而只使用属性。应该是:-

 @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

viewDidLoadGTImageView.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];
}

我在 Github 上添加了一个示例项目供您引用:- https://github.com/voyage11/PassingDataTest

关于ios - 在 iOS 中的 ViewController 之间传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23802901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com