gpt4 book ai didi

ios - prepareForSegue :sender: fails to alter property on destination VC

转载 作者:行者123 更新时间:2023-11-29 02:23:50 24 4
gpt4 key购买 nike

我不确定我做错了什么。我有两种模式——编辑和添加。在 VC1 中,当添加按钮被触摸时,它会转到 VC2,用户可以填写详细信息并向 VC1 的表中添加条目。这行得通。但是,我希望显示“添加模式”的 VC2 中有一些 ProblamaticUILabel。我尝试在 segue 中设置它,但这不起作用。

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

我使用 Storyboard将这些连接起来。

下面是一些选择输出

  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 数据的 x 行以从 VC1 填充到 VC2 的最佳方式是什么?

最佳答案

您的问题可能是因为当您尝试在 UILabel 上设置文本属性时,它还没有被初始化。 prepareForSegue: 方法在转换之前调用,因此在从 Storyboard加载任何内容之前,以及在 VC2 上调用 viewDidLoad 之前。

试试这个:

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

• 在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";
}

关于ios - prepareForSegue :sender: fails to alter property on destination VC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27794266/

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