gpt4 book ai didi

ios - 目标 ViewController 在 segue 后没有改变

转载 作者:行者123 更新时间:2023-11-28 20:00:09 25 4
gpt4 key购买 nike

我的目标 ViewController 有许多标签和图像需要根据特定的先前 ViewController 进行更改。我有一个 segue 设置,它应该改变这些但由于某种原因它不是,即使正在调用 segue。

我的初始 ViewController 有一个按钮,它有一个 segue 传递给下一个名为“aName”的 ViewController。我可能遗漏了一些简单的东西,但我一直在关注我的代码的另一部分,它做同样的事情并且有效!我还测试了使用返回正常的 NSLog 成功调用了 segue。所有 IBOutlets 也被正确分配。

初始 View Controller .m:

#import "dViewController.h"

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"aName"]) {
dViewController *dVC = (dViewController *)segue.destinationViewController;
dVC.TitleLabel.text = @"abc";
dVC.1Img.image = [UIImage imageNamed:@"somepath.jpg"];
dVC.2Img.image = [UIImage imageNamed:@"somepath2.jpg"];
[dVC.1Button setTitle:@"abcd" forState:UIControlStateNormal];
[dVC.1Button setTitle:@"efgh" forState:UIControlStateNormal];

}

目标 View Controller :

dViewController.h

    @property (strong, nonatomic) IBOutlet UILabel *TitleLabel;
@property (strong, nonatomic) IBOutlet UIImageView *1Img;
@property (strong, nonatomic) IBOutlet UIImageView *2Img;
@property (strong, nonatomic) IBOutlet UIButton *1Button;
@property (strong, nonatomic) IBOutlet UIButton *2Button;

编辑:通过多做一些环顾四周,我认为我应该使用委托(delegate)。这看起来很复杂,而且不太确定我从哪里开始。但我会看看这个线程中的内容:Changing label's text in another view controller

最佳答案

您必须为每个要更改的标签创建一个变量,并在 viewDidLoad 中将变量分配给 label.text

例如

 - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if ([segue.identifier isEqualToString:@"aName"]) {
dViewController *dVC = (dViewController *)segue.destinationViewController;
dVC.titleString = @"abc";
dVC.1Image = [UIImage imageNamed:@"somepath.jpg"];
dVC.2Image = [UIImage imageNamed:@"somepath2.jpg"];
dVC.1ButtonString=@"ButtonTitle";
dVC.2ButtonString=@"ButtonTitle2";

}

在目标 Controller 中

    @property (strong, nonatomic) IBOutlet UILabel *TitleLabel;    
@property (strong, nonatomic) IBOutlet UIImageView *1Img;
@property (strong, nonatomic) IBOutlet UIImageView *2Img;
@property (strong, nonatomic) IBOutlet UIButton *1Button;
@property (strong, nonatomic) IBOutlet UIButton *2Button;

@property(strong,nonatomic)NSString *2ButtonString;
@property(strong,nonatomic)UIImage*1Image;
@property(strong,nonatomic)UIImage*2Image;
@property(strong,nonatomic)NSString* titleString;
@property(strong,nonatomic)NSString *1ButtonString;
@property(strong,nonatomic)UIImageView *1Img;
@property(strong,nonatomic)UIImageView *1Img;



- (void)viewDidLoad{

self.TitleLabel.text = self.titleString;

self.1Img.image = self.1Image;

self.2Img.image = self.2Image;

[self.1Button setTitle:self.1ButtonString];

[self.2Button setTitle:self.1ButtonString];

}

希望对你有帮助,问题是用

 dViewController *dVC = (dViewController *)segue.destinationViewController; 

您只带 Controller 而不带 View 。并且 IBOutlet 属于 View 并且 View 未加载我希望你理解

关于ios - 目标 ViewController 在 segue 后没有改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24538538/

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