0

我想创建一个游戏,基本上是让你的伙伴说一句话。你会猜到输入了什么单词。出于这个原因,我必须将guesword from 传递给FirstViewControllerto SecondViewController

第一个视图控制器

- (IBAction)doneBtn:(id)sender {
    self.guessWord = [enterTF.text lowercaseString];
    NSLog (@"Guessword s %@", guessWord);
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    NSLog (@"%@", [segue identifier]);
    if ([[segue identifier] isEqualToString:@"F2S"])
    {
        // Get reference to the destination view controller
        SecondViewController *gvc = [segue destinationViewController];

        // Pass any objects to the view controller here, like...
        gvc.guessWord =self.guessWord;
    }

}

第二个视图控制器

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSLog(@"%@",guessWord);
}

SecondViewController猜测词中输出“null”。为什么我会为空?

在此处输入图像描述

4

2 回答 2

0

在第二个视图控制器中,在 .h 文件中声明你的变量,比如字符串类型:

 @property (nonatomic, strong) NSString *someText;

在 .m 文件中合成它:

  @synthesize someText;

在第一个视图控制器的 segue 方法中:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
NSLog (@"%@", [segue identifier]);
if ([[segue identifier] isEqualToString:@"F2S"])
{
    // Get reference to the destination view controller
    SecondViewController *gvc = [segue destinationViewController];


    gvc.someText = **YOUR TEXT HERE** ; //this should set the value in the second view controller
}

}

如果传递的值不是字符串,则进行相应更改。

于 2013-09-12T10:46:53.540 回答
0

以下是代码中发生的事件顺序:

  1. segue 对象被创建。在 segue 上设置源视图控制器和目标视图控制器,从而加载第二个视图控制器。
  2. -viewDidLoad:在第二个视图控制器上调用。
  3. -prepareForSegue:在第一个视图控制器上调用。
  4. -viewWillAppear:-viewDidAppear:在第二个视图控制器上调用。

-viewDidLoad:guessWord在配置之前调用-prepareForSegue:。在记录时尝试-viewWillAppear:-viewDidAppear:代替guessWord

于 2016-12-31T16:57:11.447 回答