gpt4 book ai didi

iphone - 在另一个类中使用一个类中的字符串变量

转载 作者:行者123 更新时间:2023-12-03 20:40:33 25 4
gpt4 key购买 nike

菜鸟问题:我正在编写一个程序,它将生成特定的字符串,然后将其显示在不同 View Controller 的文本窗口中。我一直在测试以确保代码实际上使用 NSLog 命令生成字符串,并且我知道代码正在按预期工作。由于某种原因,它没有通过 View Controller 传输,我不明白为什么。有什么帮助吗?这是代码片段:

CreateStoryViewController.m

 - (IBAction)makeStory:(id)sender 
{
StoryLine *myStory =[[StoryLine alloc] init];

[myStory setStory];
self.story = myStory.plot;
NSLog(@"story is %@", self.story);//this is generating the correct story string


self.displayStoryController = [[BIDDisplayStoryViewController alloc] initWithNibName:@"DisplayStoryView" bundle:nil];

[self.view insertSubview:self.displayStoryController.view atIndex:1];



}

DisplayStoryViewController.m

    - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

BIDCreateStoryViewController *newStory = [[BIDCreateStoryViewController alloc] init];
NSLog(@"newStory.story is %@",newStory.story);//this generates null message
self.storyDisplay.text = newStory.story;

}

最佳答案

这是错误的。您正在第二个 View Controller 中实例化一个新的 BIDCreateViewController 对象。这与推送第二个 BIDDisplayStoryViewController 的原始 BIDCreateViewController 对象不同。

您需要在 BIDDisplayStoryViewController 的头文件中声明一个字符串属性。

类似

@property (nonatomic, retain /*or strong, if using ARC*/) NSString *storyToDisplay;

请务必将其综合到您的实现文件中。

当您在第一个 View Controller 中创建 BIDDisplayStoryViewController 时,您需要执行以下操作:

self.displayStoryController = [[BIDDisplayStoryViewController alloc] initWithNibName:@"DisplayStoryView" bundle:nil];
self.displayStoryViewController.storyToDisplay = self.story;

现在,在第二个 View Controller 中,您可以使用 self.myStory 访问它。

虽然这可以解决您的问题(请理解,我无意在这里表现得无礼),但我觉得大家对 iOS(以及一般的 OOP)的工作原理缺乏了解。

关于iphone - 在另一个类中使用一个类中的字符串变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17603899/

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