xcode Objective-C (iOS):prepareForSegue 不会将我的数据传递到目标 VC

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11322431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:46:39  来源:igfitidea点击:

Objective-C (iOS): prepareForSegue won't pass my data into destination VC

objective-ciosxcode

提问by brianSan

VC1 = NewGameViewController
VC2 = GameViewController

VC1 = NewGameViewController
VC2 = GameViewController

NewGameViewController.m

NewGameViewController.m

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if( [segue.identifier isEqualToString:@"newGameSegue"]) {
    GameViewController *gameVC = (GameViewController *)segue.destinationViewController;

    NSArray *array = [self nameArrayForTextFieldArray:self.namePicker.textFieldArray withColon:YES];
    gameVC.nameArray = [[NSArray alloc] initWithArray:array];
}

-(NSArray *)nameArrayForTextFieldArray:(NSArray *)array withColon:(BOOL *)boolbasically returns an nsarray of strings given an nsarray of textfields. withcolon is a bool of whether or not you want the strings to have a colon appended to the end.

-(NSArray *)nameArrayForTextFieldArray:(NSArray *)array withColon:(BOOL *)bool给定一个文本字段的 nsarray,基本上返回一个字符串的 nsarray。withcolon 是一个布尔值,表示您是否希望字符串的末尾附加一个冒号。

when i debug my code, the _nameArray ivar in gameVC still reads nil after every line here is called...can anyone help me out here??

当我调试我的代码时,gameVC 中的 _nameArray ivar 在调用这里的每一行后仍然读取 nil ......有人可以帮我吗?

回答by andreapavan

The prepareForSegue method is invoked by UIKit when a segue from one screen to another is about to be performed. It allows us to give data to the new view controller before it will be displayed. Usually you'll do that by setting its properties.

当即将执行从一个屏幕到另一个屏幕的转场时,UIKit 会调用 prepareForSegue 方法。它允许我们在显示之前将数据提供给新的视图控制器。通常你会通过设置它的属性来做到这一点。

The new view controller can be found in segue.destinationViewController. If GameViewController embed the navigation controller, the new view controller will not be GameViewController but the navigation controller that embeds it. To get the GameViewController object, we can look at the navigation controller's topViewController property. This property refers to the screen that is currently active inside the navigation controller.

新的视图控制器可以在 segue.destinationViewController 中找到。如果 GameViewController 嵌入了导航控制器,新的视图控制器将不是 GameViewController 而是嵌入它的导航控制器。要获取 GameViewController 对象,我们可以查看导航控制器的 topViewController 属性。此属性指的是导航控制器内当前处于活动状态的屏幕。

To send an object to the new view controller you can use this solution using performSegueWithIdentifier:

要将对象发送到新的视图控制器,您可以使用以下解决方案performSegueWithIdentifier

For example, if we want to perform a segue pressing a UIButtonwe can do this:

例如,如果我们想按 a 执行转场,UIButton我们可以这样做:

In the MyViewController.hwe create a IBAction (connected to UIButton), dragging the button from storyboard to code:

MyViewController.h我们创建一个 IBAction(连接到 UIButton),将按钮从 storyboard 拖到代码中:

- (IBAction)sendData:(id)sender;

In MyViewController.mwe implement the method:

MyViewController.m我们实现方法中:

- (IBAction)sendData:(id)sender
{
    NSArray *array = [self nameArrayForTextFieldArray:self.namePicker.textFieldArray withColon:YES];
    [self performSegueWithIdentifier:@"newGameSegue" sender:array];
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"newGameSegue"]) {
        UINavigationController *navigationController = segue.destinationViewController;
        GameViewController *controller = (GameViewController *)navigationController.topViewController;
        controller.nameArray = sender;
    }
}

回答by Scott Berrevoets

Is GameViewController embedded in a navigation controller? In that case, your destinationViewController property is of type UINavigationController, not GameViewController. You can get to GameViewController by calling [segue.destinationViewController.viewControllers lastObject].

GameViewController 是否嵌入在导航控制器中?在这种情况下,您的 destinationViewController 属性属于 UINavigationController 类型,而不是 GameViewController。您可以通过调用来访问 GameViewController [segue.destinationViewController.viewControllers lastObject]

回答by Rob

I'm assuming that you've done a NSLog (or examine it in the debugger) of arrayimmediately before setting gameVC.nameArray. You really want to make sure it's being set the way you think it is. It's amazing how many times I've spent debugging something like this only to realize the problem was in my equivalent to nameArrayForTextFieldArray. Or a typo in the name of the segue identifier. Or random things like that.

我假设您array在设置gameVC.nameArray. 您真的想确保它按照您认为的方式进行设置。令人惊讶的是,我花了多少次调试这样的东西才意识到问题出在我相当于nameArrayForTextFieldArray. 或者 segue 标识符名称中的拼写错误。或者诸如此类的随机事件。

Assuming that's ok, then a couple of things are possible:

假设那没问题,那么有几件事是可能的:

  1. How is your nameArrayproperty defined in GameViewController? If it's not a strongreference (or a copy), then when your arrayfalls out of scope, it will be deallocated. I think this would manifest itself slightly differently, but it's worth confirming.

  2. Also, I've seen situations where a controller like GameViewControllermight have some confusion between various ivars and properties (which is why I neverdefine ivars for my properties ... I let @synthesizedo that).

  3. I assume you're not using a custom setter for nameArray. I just want to make sure. If so, though, please share that, too.

  1. 你的nameArray财产是如何定义的GameViewController?如果它不是strong引用(或 a copy),那么当您array超出范围时,它将被释放。我认为这会略微不同,但值得确认。

  2. 另外,我见过这样的情况,控制器GameViewController可能在各种 ivars 和属性之间存在一些混淆(这就是为什么我从不为我的属性定义 ivars ......我让@synthesize这样做)。

  3. 我假设您没有为nameArray. 我只是想确定一下。不过,如果是这样,请也分享一下。

Bottom line, can you show us all references to nameArrayin your @interfaceof GameViewControlleras well as in its @synthesizestatement?

最重要的是,您能否向我们展示nameArray您的@interfaceofGameViewController及其@synthesize声明中的所有引用?