繁体   English   中英

prepareForSegue:sender:无法更改目标VC上的属性

[英]prepareForSegue:sender: fails to alter property on destination VC

我不确定自己在做什么错。 我有两种模式-编辑和添加。 在VC1中,当按下添加按钮时,它将选择VC2,用户可以填写详细信息并将条目添加到VC1的表中。 这可行。 但是,我在VC2中有一些ProblamaticUILabel,希望显示“添加模式”。 我尝试在segue中设置它,但这不起作用。

如果用户然后触摸他们在VC1中创建的表单元格,则我希望使用相同的VC2,但我想将someProblamaticUILabel更改为“编辑模式”。 这是行不通的。

我使用情节提要将它们连接起来。

以下是一些选择输出

  From Segue of VC1
     segue.destinationViewController: AppVC2: <AppVC2: 0x146b5f40>
     AppVC2.someProblamaticUILabel: (null)
     AppVC2.someProblamaticUILabel.text: (null)
  From viewDidLoad of VC2: <AppVC2: 0x146b5f40>
     AppVC2 someFunctionalAttribute: edit
     AppVC2 someProblamaticUILabel: <UILabel: 0x1454efc0; frame = (16 20; 151 54); text = 'Add/Edit'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x1454ef50>>
     AppVC2 someProblamaticUILabel.text: Add/Edit

一些代码

// VC1

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.identifier isEqualToString:@"AddHomePlayerSegue" ]){
        AppVC2 * addHome = (AppVC2 *) segue.destinationViewController;
        [addHome setDelegate:self];
        addHome.teamType = HOME_TEAM;
        addHome.mode = ADD_MODE;
        addHome.someProblamaticUILabel.text = @"Add";

    }else if([segue.identifier isEqualToString:@"EditVisitorPlayerSegue" ]){
        AppVC2 * editVisitor = (AppVC2 *) segue.destinationViewController;
        [editVisitor setDelegate:self];
        editVisitor.mode = EDIT_MODE;
        editVisitor.someProblamaticUILabel.text = @"Edit";

        NSLog(@"\n  From Segue of VC1");
        NSLog(@"\n     segue.destinationViewController: AppVC2: %@", editVisitor);
        NSLog(@"\n     AppVC2.someProblamaticUILabel: %@", editVisitor.someProblamaticUILabel);
        NSLog(@"\n     AppVC2.someProblamaticUILabel.text: %@", editVisitor.someProblamaticUILabel.text);
    }
}

// VC2

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"\n  From viewDidLoad of VC2: %@", self);
    NSLog(@"\n     AppVC2 someFunctionalAttribute: %@", self.mode);
    NSLog(@"\n     AppVC2 someProblamaticUILabel: %@", self.someProblamaticUILabel);
    NSLog(@"\n     AppVC2 someProblamaticUILabel.text: %@", self.someProblamaticUILabel.text);
}

另一个问题是告诉VC2用户用y数据从VC1填充到VC2的x行的最佳方法是什么?

您的问题可能来自以下事实:当您尝试在UILabel上设置text属性时,它尚未被初始化。 在过渡之前,因此在从情节提要加载任何内容之前以及在VC2上调用viewDidLoad之前,都会调用prepareForSegue:方法。

尝试这个 :

•删除两行addHome.someProblamaticUILabel.text = @"....."; 从您的prepareForSegue:方法。

•在VC2中,在viewDidLoad方法中,测试self.mode并根据以下值设置标签:

-(void)viewDidLoad {
    [super viewDidLoad];

    if ([self.mode isEqualToString:ADD_MODE]) self.someProblamaticUILabel.text = @"Add";
    else if ([self.mode isEqualToString:EDIT_MODE]) self.someProblamaticUILabel.text = @"Edit";
    else self.someProblamaticUILabel.text = @"Unknown";
}

您应该检查在其中调用方法的顺序。

首先,将会有

[self performSegueWithIdentifier:@"SEGUE_IDENTIFIER"];

其次,将调用目标视图控制器的viewDidLoad

第三,第一个视图控制器中的prepareForSegue将被调用。

与其检查目标视图控制器的viewDidLoad中的属性, 不如将它们记录在目标视图控制器的viewDidAppear中。

希望这可以帮助..

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM