【问题标题】:How do I pass variables between view controllers?如何在视图控制器之间传递变量?
【发布时间】:2008-12-20 08:36:18
【问题描述】:

我在使用 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 文件;在这种情况下,请询问。

提前致谢, 哈尔

PS:尝试使用其他变量(也包括其他类型),清理/重建,重新创建 xib 文件,你可以命名它......我没有技巧:(

【问题讨论】:

  • 如果您将标题更改为更具描述性,您可能会得到更好的回复。例如“无法在 iPhone 项目中的控制器之间传递变量”

标签: objective-c cocoa-touch


【解决方案1】:

首先,不要使用-&gt;——这是直接的实例变量访问。它可能有效,但是您在它不知情的情况下更改了另一个对象的实例变量,这只是自找麻烦。

不,Adam Rosenfield 不是指dvc-&gt;array_resultados;他的意思是resultadosControllerCell-&gt;array_resultados,这是他说的,也是他根据你说的。

正确的解决方案是混合您的原始线路和您对亚当线路的修订:

dvc.array_resultados = [self array_resultados];

这会通过您在 DetalhesViewController 类中声明的属性。

说到这一点,您应该将该属性声明为copy,而不是retain。否则,您会发现自己持有其他人的可变数组,然后他们将对其进行修改——更糟糕的魔力。

【讨论】:

    【解决方案2】:

    这并不能准确回答您的问题,但您的内存管理有点不稳定。这一行:

    [dvc dealloc];
    

    应该是这样的:

    [dvc release];
    dvc = nil;
    

    在 Cocoa 中,您永远不应该直接调用 dealloc - 遵循 retain/release/autorelease 模式,事情会按照预期更好地工作。

    【讨论】:

      【解决方案3】:

      只是猜测,但您是否尝试过使用箭头运算符 -&gt; 而不是点运算符 .

      resultadosControllerCell->array_resultados  = [self array_resultados];
      

      【讨论】:

        【解决方案4】:

        好的,我已经吃过了;使用便宜的技巧来显示信息:

        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];
        

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

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

        干杯!

        【讨论】:

          【解决方案5】:

          一种更有效的方法是将来自两个视图控制器的数据分离到一个模型中。您将有一个名为 ResultadoModel.h/m 的单独类 (NSObject)。这将是一个单例,因此两个类都可以访问同一个实例。

          您可以通过执行以下操作(在两个 vcs 中)来访问数组:

          [[[ResultadoModel sharedInstance] array_resultados] propertyOrMethod];
          

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

          【讨论】:

            【解决方案6】:

            请按照这些简单的步骤,它应该工作。

            在您的 SecondViewController 中:

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

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

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

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

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

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

            希望这会有所帮助。

            莫妮卡ツ

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2012-07-19
              • 1970-01-01
              • 2014-11-19
              • 1970-01-01
              • 1970-01-01
              • 2015-07-22
              • 1970-01-01
              • 2018-07-03
              相关资源
              最近更新 更多