如何在视图控制器之间传递变量?

4
我在 Xcode 方面遇到了困难;出于某种原因,它不允许我将一个变量从一个视图控制器类传递到另一个视图控制器类。它应该能够工作,我基本上只是从我的其他类中复制/粘贴(对它们都起作用……除了这个)。我已经一整晚都在努力尝试着解决它,尝试了我能想到的所有方法,但仍然无法解决。
这是我正在进行调用的视图控制器类:

ResultadosViewController.h:

#import <UIKit/UIKit.h>
#import "Filme.h"
#import "Festival.h"
#import "Top10Discos.h"
#import "Peca.h"

@class DetalhesViewController;

@interface ResultadosViewController : UIViewController
{
    // Navegation
    DetalhesViewController *dvc;
    BOOL isViewPushed;

    // What i'd really like to pass lol
    NSArray *array_resultados;

}

@property (nonatomic, retain) NSArray *array_resultados;
@property (nonatomic, readwrite) BOOL isViewPushed;

@end*

ResultadosViewController.m:

#import "ResultadosViewController.h"
#import "DetalhesViewController.h"
#import "Filme.h"
#import "Peca.h"
#import "Top10Discos.h"
#import "Festival.h"

@implementation ResultadosViewController
@synthesize isViewPushed, array_resultados;

(...)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic -- create and push a new view controller

    if (indexPath.row == 2)
    {
        if(dvc != nil)
            [dvc dealloc];

        NSString *ffs = [[array_resultados objectAtIndex:indexPath.row] TituloFilme];

        dvc = [[DetalhesViewController alloc] initWithNibName:@"DetailedView" bundle:[NSBundle mainBundle]];

        **resultadosControllerCell.array_resultados  = [self array_resultados];** 
        *"Request for member 'array_resultados' in something not a structure or union"*

        //Push the view controller to the top of the stack.
        [self.navigationController pushViewController:dvc animated:YES];

    }
}

这是我想要将数组发送到的另一个类:

DetalhesViewController.h:

#import <UIKit/UIKit.h>

#import "Filme.h"
#import "Festival.h"
#import "Top10Discos.h"
#import "Peca.h"


@interface DetalhesViewController : UIViewController
{
    // Navegacao
    NSArray *array_resultados;

}

@property (nonatomic, retain) NSArray *array_resultados;
@end

我不确定你们中是否有人想要查看此类的.m文件;如果是这样,请询问。
提前致谢, Hal
附注:尝试使用其他变量(也尝试了其他类型),清理/重建,重新创建xib文件等等……我已经没有更多的技巧可用 :(

如果您将标题更改为更具描述性的内容,例如“无法在iPhone项目中在控制器之间传递变量”,则可能会获得更好的回复。 - Abizern
6个回答

4
首先,不要使用->——那是直接实例变量访问。这可能有效,但你正在改变另一个对象的实例变量,而它并不知情,这只会带来麻烦。
不,Adam Rosenfield不是指dvc->array_resultados;他指的是resultadosControllerCell->array_resultados,这就是他说的,并且这也是他基于你所说的内容得出的结论。
正确的解决方案是你原来代码和你修改后的Adam代码的混合体:
dvc.array_resultados = [self array_resultados];

这里涉及到你在DetalhesViewController类中声明的属性。

顺便说一下,你应该将该属性声明为copy,而不是retain。否则,你会发现自己拥有别人的可变数组,他们会对其进行修改——这会带来更多的问题。


3

虽然这并不完全回答了你的问题,但是你的内存管理有点混乱。这一行代码:

[dvc dealloc];

应该这样阅读:
[dvc release];
dvc = nil;

在Cocoa中,你不应该直接调用dealloc方法——遵循retain/release/autorelease模式,事情会更好地按照预期运行。

0
更加专业的做法是将数据从视图控制器中分离出来,放到一个模型中。你需要创建一个名为ResultadoModel.h/m的单例类(NSObject),这样两个类就可以访问同一个实例。
你可以通过以下方式在两个视图控制器中访问该数组:
[[[ResultadoModel sharedInstance] array_resultados] propertyOrMethod];

你可以搜索如何创建单例,它非常简单而且非常强大。


0

请按照以下简单步骤操作,应该可以解决问题。

在您的SecondViewController中:

  1. 创建第二个视图控制器中的初始化程序。例如:

    -(id)initWithResultsArray: (NSArray *) resultsArray;

  2. 创建一个变量来保存数组。比如说myResultsArray

  3. initWithResultsArray方法中,将resultsArray的值保存到myResultsArray中。

  4. 使用initWithResultsArray而不是仅使用init来初始化SecondViewController

  5. 像往常一样呈现您的控制器,您将能够使用myResultsArray

希望这可以帮助您。

Monica ツ


0
猜测一下,你是否尝试使用箭头运算符->而不是点运算符.
resultadosControllerCell->array_resultados  = [self array_resultados];

0

好了,我受够了;使用一个廉价的技巧来显示信息:

UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:ffs message:@"this gets covered" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    UILabel *myTextField = [[UILabel alloc] init];
    myTextField.text = @"FFS!";
    CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, 130.0);
    [myAlertView setTransform:myTransform];

    [myTextField setBackgroundColor:[UIColor whiteColor]];
    [myAlertView addSubview:myTextField];
    [myAlertView show];
    [myAlertView release];

真的很讨厌使用这个,但我已经晚了一天。我应该在昨天交付它。

感谢大家的帮助,如果你们找到解决方案,请告诉我。 你永远不知道它是否会再次发生 :/

干杯!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接