Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 通过ViewController传递数据不工作_Ios_Objective C_Uiviewcontroller_Viewcontroller_Message Passing - Fatal编程技术网

Ios 通过ViewController传递数据不工作

Ios 通过ViewController传递数据不工作,ios,objective-c,uiviewcontroller,viewcontroller,message-passing,Ios,Objective C,Uiviewcontroller,Viewcontroller,Message Passing,好的,我正试图通过下面的代码将数据从一个视图控制器传递到另一个视图控制器,但它不起作用,我不知道为什么 在我的ViewController.m中,我导入了ViewControllerTwo.h并声明了ViewControllerTwo: #import "ViewController.h" #import "ViewControllerTwo.h" @interface ViewController () @end @implementation V

好的,我正试图通过下面的代码将数据从一个视图控制器传递到另一个视图控制器,但它不起作用,我不知道为什么

在我的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。在前面的my 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)prepareforsgue:(UIStoryboardSegue*)分段发送方:(id)发送方

然后命名您的segue,然后执行以下操作:

if([segue.identifier  isEqual: @"segue_From_1_To_2"])
{
    ViewControllerTwo *vc2 = segue.destinationViewController;
    vc2.timerValue = 123.45;
    vc2.text = @"whatever";
    vc2.blockCount = 1;
}

在何处初始化
设置
变量以指向实际的
ViewControllerTwo
对象?我对其进行了更新,将我的viewDidLoad方法包含在放置它的位置。这可能是因为我在ViewControllerTwo.m中合成属性之前设置了属性值吗?我怀疑您创建为
settings
ViewControllerTwo
的实例是否与您正在测试的相同。如果在为其赋值时记录
设置的值
,在
查看控制器时记录
自身的值
,则两个
打印其
区块计数
,它们是否显示相同的地址?它们不显示。在my ViewController.m中,settings.text的地址是:2015-01-11 17:30:24.213 Spreed[7688:157905]0x7f92a2435260。在我的ViewController中,self.text的两个地址是2015-01-11 17:30:26.603 Spreed[7688:157905]0x0。如何确保我在ViewController中的实例引用了我在storyboard中制作的两个ViewController?是否使用segue?