【问题标题】:Segue data transferSegue 数据传输
【发布时间】:2013-06-30 23:23:51
【问题描述】:

我试过一次,但我觉得我解释得不够清楚。我是新手,所以请原谅我缺乏术语。

我正在尝试通过模态 segue 将 UItextlabel 文本中的数据传递到第二个视图控制器,该控制器将其内容计算为两个答案。哦应该提到它是基于故事板的!

看起来像这样:

(FirstView 控制器)

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
NSLog(@"prepareForSegue: %@", segue.identifier);
if
    ([segue.destinationViewController isKindOfClass:[SampleDelayCalculator2ViewController class]])
{
    D = d1.text;
    TT = T1.text;
    FQ = Fq1.text;

    NSLog(@"Transfering Data");

    //sending information to new view controller with a segue
    SampleDelayCalculator2ViewController *viewcontrollerB = (SampleDelayCalculator2ViewController*)segue.destinationViewController;
    viewcontrollerB.d.text=self.D;
    viewcontrollerB.T.text=self.TT;
    viewcontrollerB.Fq.text=self.FQ;        
}
return;
}

这直接从 UITextLabels 读取,并在每次触发“计算”按钮时执行。

然后它进入第二个视图

- (void)viewDidLoad
{            
[self calculate]; 
[super viewDidLoad];
}



-(void) calculate{


//asigning the values from prepare for segue method to floats

NSLog(@"Value of string is %@", T.text);
Tf = ([T.text floatValue]);
Fqf = ([Fq.text floatValue]);
df = ([d.text floatValue]);
NSLog(@"Value of string is %f", Tf);
...

然后它继续到别的东西。 我的主要问题是,每当打印 T.text 或 Tf 时,它都会显示 nill 或 0.00000,我显然没有放入 UITextLabel。我怎么解决这个问题?

我尝试在 ViewDidAppear 中实现计算,但这并没有改变任何东西。

我希望我说清楚了:D

【问题讨论】:

  • 如果您想了解更多有关代码的信息,请询问!

标签: uistoryboardsegue data-transfer


【解决方案1】:

试试这样的:

请务必在情节提要中为您的 segue 命名。

在您的第一个视图控制器中:

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier]isEqualToString:@"segueToHomeDetails"]) {
        SampleDelayCalculator2ViewController *viewcontrollerB = (SampleDelayCalculator2ViewController*)segue.destinationViewController;

        viewcontrollerB.d = d1.text; // don't use viewcontrollerB.d.text it won't set the text in the next controller
        viewcontrollerB.T = T1.text;
        viewcontrollerB.Fq = Fq1.text;
    }
}

在您的第二个视图控制器中: 使用您在第二个 viewcontroller.h 文件中设置的属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多