通过 ViewControllers 传递数据不起作用
Passing Data Through ViewControllers Not Working
好的,所以我尝试通过以下代码将数据从一个视图控制器传递到另一个视图控制器,但它不起作用,我不知道为什么。
在我的 ViewController.m 中,我导入了 ViewControllerTwo.h 并声明了 ViewControllerTwo:
#import "ViewController.h"
#import "ViewControllerTwo.h"
@interface ViewController ()
@end
@implementation ViewController
{
ViewControllerTwo *settings;
}
@synthesize blockSizeLB;
@synthesize wpmLB;
@synthesize textTV;
@synthesize slider;
@synthesize stepper;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//sets label to corresponding value
wpmLB.text = [NSString stringWithFormat:@"WPM: %@", [[NSNumber numberWithInt:slider.value] stringValue]];
//configure stepper and its label to default values of 1
stepper.value = 1;
blockSizeLB.text = [NSString stringWithFormat:@"Block Size: %@", [[NSNumber numberWithInt:stepper.value] stringValue]];
//sets properties for next ViewController
settings = [[ViewControllerTwo alloc] init];
settings.timerValue = 60 / slider.value;
settings.text = textTV.text;
settings.blockCount = stepper.value;
}
在 ViewControllerTwo.h 我有:
@property (nonatomic) float timerValue;
@property (nonatomic) NSString * text;
@property (nonatomic) int blockCount;
稍后在 ViewController.m 我需要更改 ViewControllerTwo 中定义的属性:
来自 ViewController.m 的方法。这也是早些时候在我的 viewDidLoad 中完成的,以设置属性的默认值:
- (IBAction)sliderSlide:(id)sender
{
//event when the slider value is changed
//rounds value to nearest increment of 5
int increment = 5 * floor(((int)slider.value/5)+0.5);
[slider setValue:increment animated:YES];
//changes WPM: 0 label text
wpmLB.text = [NSString stringWithFormat:@"WPM: %@", [[NSNumber numberWithInt:increment] stringValue]];
//sets properties for next ViewController
settings.timerValue = 60 / slider.value;
}
我尝试通过调用 ViewControllerTwo.m 中的方法来测试这是否成功,该方法通过 NSLog 记录其 属性 blockCount。然而,输出是 (null) 意味着我未能成功将数据从 ViewController.m 传递到 ViewControllerTwo
您可以在 viewControllerTwo 中创建自定义初始化程序。将其命名为 initWithTimerValue:
- (id)initWithTimerValue:(CGFloat)timerValue
{
self = [super init];
if (self) {
self.timerValue = timerValue;
}
return self;
}
如果您正在使用 segues,您应该在以下位置执行此操作:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
然后命名你的 segue,然后做这样的事情:
if([segue.identifier isEqual: @"segue_From_1_To_2"])
{
ViewControllerTwo *vc2 = segue.destinationViewController;
vc2.timerValue = 123.45;
vc2.text = @"whatever";
vc2.blockCount = 1;
}
好的,所以我尝试通过以下代码将数据从一个视图控制器传递到另一个视图控制器,但它不起作用,我不知道为什么。
在我的 ViewController.m 中,我导入了 ViewControllerTwo.h 并声明了 ViewControllerTwo:
#import "ViewController.h"
#import "ViewControllerTwo.h"
@interface ViewController ()
@end
@implementation ViewController
{
ViewControllerTwo *settings;
}
@synthesize blockSizeLB;
@synthesize wpmLB;
@synthesize textTV;
@synthesize slider;
@synthesize stepper;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//sets label to corresponding value
wpmLB.text = [NSString stringWithFormat:@"WPM: %@", [[NSNumber numberWithInt:slider.value] stringValue]];
//configure stepper and its label to default values of 1
stepper.value = 1;
blockSizeLB.text = [NSString stringWithFormat:@"Block Size: %@", [[NSNumber numberWithInt:stepper.value] stringValue]];
//sets properties for next ViewController
settings = [[ViewControllerTwo alloc] init];
settings.timerValue = 60 / slider.value;
settings.text = textTV.text;
settings.blockCount = stepper.value;
}
在 ViewControllerTwo.h 我有:
@property (nonatomic) float timerValue;
@property (nonatomic) NSString * text;
@property (nonatomic) int blockCount;
稍后在 ViewController.m 我需要更改 ViewControllerTwo 中定义的属性: 来自 ViewController.m 的方法。这也是早些时候在我的 viewDidLoad 中完成的,以设置属性的默认值:
- (IBAction)sliderSlide:(id)sender
{
//event when the slider value is changed
//rounds value to nearest increment of 5
int increment = 5 * floor(((int)slider.value/5)+0.5);
[slider setValue:increment animated:YES];
//changes WPM: 0 label text
wpmLB.text = [NSString stringWithFormat:@"WPM: %@", [[NSNumber numberWithInt:increment] stringValue]];
//sets properties for next ViewController
settings.timerValue = 60 / slider.value;
}
我尝试通过调用 ViewControllerTwo.m 中的方法来测试这是否成功,该方法通过 NSLog 记录其 属性 blockCount。然而,输出是 (null) 意味着我未能成功将数据从 ViewController.m 传递到 ViewControllerTwo
您可以在 viewControllerTwo 中创建自定义初始化程序。将其命名为 initWithTimerValue:
- (id)initWithTimerValue:(CGFloat)timerValue
{
self = [super init];
if (self) {
self.timerValue = timerValue;
}
return self;
}
如果您正在使用 segues,您应该在以下位置执行此操作:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
然后命名你的 segue,然后做这样的事情:
if([segue.identifier isEqual: @"segue_From_1_To_2"])
{
ViewControllerTwo *vc2 = segue.destinationViewController;
vc2.timerValue = 123.45;
vc2.text = @"whatever";
vc2.blockCount = 1;
}