I know this question was asked about a 100 times but I just can't figure out what I'm doing wrong.
我知道这个问题被问了大约100次,但我无法弄清楚我做错了什么。
This ist my second iOS App. I want to modify a Label in my ViewControllerB by pressing a Button in my ViewControllerA.
这是我的第二个iOS应用程序。我想通过按ViewControllerA中的Button来修改ViewControllerB中的Label。
- I made a Segue in Storyboard from my Button (
ViewControllerA
) to myViewControllerB
and called it "TestSegue". - 我在Storyboard中从我的Button(ViewControllerA)创建了一个Segue到我的ViewControllerB并称之为“TestSegue”。
- Wrote
#import "ViewControllerB.h
in myViewControllerA.h
- 在我的ViewControllerA.h中写了#import“ViewControllerB.h”
- I wrote this Code in my ViewControllerA.m :
- 我在ViewControllerA.m中编写了这段代码:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:@"TestSegue"]){
ViewControllerB *controller = (ViewControllerB *)segue.destinationViewController;
controller.TestLabel.text = @"TEST!";}}
In my ViewControllerB nothing happens with the TestLabel...
在我的ViewControllerB中,TestLabel没有任何反应......
Can anybody see what I did wrong?
谁能看到我做错了什么?
Thanks in advance
提前致谢
Edit: Works now thanks to rpledge.
编辑:现在感谢rpledge。
New Code:
新代码:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"TestSegue"]){
ViewControllerB *controller = (ViewControllerB *)segue.destinationViewController;
controller.TestString = @"TEST!";}}
2 个解决方案
#1
1
It'l likely because the TextLabel UI element isn't created until viewDidLoad is run, which will happen after prepareForSegue:
这很可能是因为在运行viewDidLoad之前不会创建TextLabel UI元素,这将在prepareForSegue之后发生:
I don't like exposing UI elements for views publicly anyway, I would suggest you add an @property for your NSString* to the destination view controller then set the UILabel.text in viewDidLoad:
我不喜欢公开公开视图的UI元素,我建议你将NSString *的@property添加到目标视图控制器,然后在viewDidLoad中设置UILabel.text:
#2
0
Don't assign it to the control, but to an instance variable.
不要将它分配给控件,而是分配给实例变量。
@property (nonatomic) NSString *var;
And then in the viewDidLoad assign this to the control.
然后在viewDidLoad中将此值分配给控件。
#1
1
It'l likely because the TextLabel UI element isn't created until viewDidLoad is run, which will happen after prepareForSegue:
这很可能是因为在运行viewDidLoad之前不会创建TextLabel UI元素,这将在prepareForSegue之后发生:
I don't like exposing UI elements for views publicly anyway, I would suggest you add an @property for your NSString* to the destination view controller then set the UILabel.text in viewDidLoad:
我不喜欢公开公开视图的UI元素,我建议你将NSString *的@property添加到目标视图控制器,然后在viewDidLoad中设置UILabel.text:
#2
0
Don't assign it to the control, but to an instance variable.
不要将它分配给控件,而是分配给实例变量。
@property (nonatomic) NSString *var;
And then in the viewDidLoad assign this to the control.
然后在viewDidLoad中将此值分配给控件。