[英]Destination ViewController not changing after segue
My destination ViewController has a number of labels and images which need to be changed dependent on the specific previous ViewController. 我的目标ViewController有许多标签和图像,需要根据以前的特定ViewController进行更改。 I have a segue set up which should change these but for some reason it is not, even though the segue is being called. 我已经设置了segue,应该更改这些设置,但是由于某种原因,即使正在调用segue,也不能更改这些设置。
My initial ViewController has a button which has a segue passing to the next ViewController with the name "aName". 我最初的ViewController有一个按钮,其中一个按钮会传递给名称为“ aName”的下一个ViewController。 I'm probably missing something simple but I have been following another section of my code which does the exact same thing and works! 我可能缺少一些简单的东西,但是我一直在关注代码的另一部分,该部分完全相同并且可以正常工作! I have also tested that the segue is successfully being called with a NSLog which returns fine. 我还测试了成功通过NSLog成功调用segue。 All the IBOutlets are also assigned correctly. 所有IBOutlets也已正确分配。
Initial View Controller.m: 初始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];
}
Destination ViewController: 目标ViewController:
dViewController.h 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;
Edit: From doing some more looking around I think I should be using a delegate. 编辑:从做更多环顾四周,我认为我应该使用一个委托。 It seems quite complicated and not really sure where I start. 似乎很复杂,我不确定从哪里开始。 But I'll have a go at looking at what's in this thread: Changing label's text in another view controller 但是,我将看看该线程中的内容:在另一个视图控制器中更改标签的文本
you have to create a variable for each label you want to change and in viewDidLoad assing the variable to the label.text 您必须为每个要更改的标签创建一个变量,然后在viewDidLoad中将变量赋给label.text
for example 例如
- (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";
}
in DestinationController 在DestinationController中
@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];
}
hope It help you, the problem is that with 希望对您有帮助,问题在于
dViewController *dVC = (dViewController *)segue.destinationViewController;
you only bring the controller not the view. 您只带控制器而不是视图。 and the IBOutlet belongs to the view and the view is not load i hope you understand 并且IBOutlet属于该视图,并且该视图未加载,我希望您理解
像这样在segue.destinationViewController周围加上括号:
dViewController *dVC = (dViewController *)(segue.destinationViewController);
Read about app navigation principles. 了解有关应用导航原理的信息。 From apple: 从苹果:
https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/NavigationControllers.html https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/NavigationControllers.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.