Ios 使用脚本传递参数

Ios 使用脚本传递参数,ios,storyboard,Ios,Storyboard,我想创建一个游戏,基本上是让你的朋友放一个字。你会猜到输入了什么单词。因此,我必须将单词从FirstViewController传递到SecondViewController 第一视图控制器 - (IBAction)doneBtn:(id)sender { self.guessWord = [enterTF.text lowercaseString]; NSLog (@"Guessword s %@", guessWord); } - (void)prepareForSegue

我想创建一个游戏,基本上是让你的朋友放一个字。你会猜到输入了什么单词。因此,我必须将单词从
FirstViewController
传递到
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);
}
第二视图控制器

- (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
guessword输出“null”中。为什么我会变空


在第二个视图控制器中,在.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
}

}

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

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

  • 此时将创建segue对象。在segue上设置源视图控制器和目标视图控制器,从而加载第二个视图控制器
  • -viewDidLoad:
    在第二个视图控制器上调用
  • -prepareforsgue:
    在第一个视图控制器上被调用
  • -viewwillbeen:
    -viewdidebeen:
    在第二个视图控制器上被调用

  • -prepareforsgue:
    中配置
    猜测词之前调用
    -viewDidLoad:
    。尝试
    -查看将显示:
    -查看显示:
    而不是在记录
    猜测词时显示:
    ,您的问题是什么?为什么我得到的是null,而不是输入的猜测词为什么为null…很明显,它是否击中了您试图传递它的那一行?是的,它击中了那一行。