如何在视图控制器之间传递变量? [英] How do I pass variables between view controllers?
问题描述
我有一个很难与Xcode;因为某些原因,它只是不允许我传递一个变量从一个视图控制器类到另一个。它应该工作,我基本上只是复制/粘贴从我的其他类(它适用于所有的...除了这一个)。
i'm having a hard time with Xcode; for some reason, it just won't let me pass a variable from one view controller class to another. It should work, i was basically just copying/pasting from my other classes (it works on all of them... except this one). I've been at it all night long, tried everything i could think of and still it remains.
这里是我打电话的视图控制器类:
Here's the view controller class where I'm making the call:
#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:
>
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];
}
}
发送数组到:
#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文件;
I'm not sure if any of you would to see the .m file for this class; in that case, just ask.
提前感谢,
Hal
Thanks in advance, Hal
其他变量(其他类型的),清理/重建,重新创建xib文件,你命名...我是outta技巧:(
PS: tried with other variables (other types too), cleansed/rebuilt, recreated xib file, you name it... i'm outta tricks :(
推荐答案
首先,不要使用 - >
- 直接的实例变量访问它可能工作,但是你正在改变另一个对象的实例变量,
First off, don't use ->
— that's direct instance-variable access. It may work, but you're changing another object's instance variables without its knowledge, which is just asking for trouble.
没有,Adam Rosenfield不是指 dvc-> array_resultados
;他的意思是 resultadosControllerCell-> array_resultados
,这是他说的,他是基于你说的。
And no, Adam Rosenfield didn't mean dvc->array_resultados
; he meant resultadosControllerCell->array_resultados
, which is what he said and which he based on what you said.
正确的解决方案是原始行和您对Adam的行的修订版本的混合:
The correct solution is a blend of your original line and your revision of Adam's line:
dvc.array_resultados = [self array_resultados];
这是通过你在 DetalhesViewController
class。
This goes through the property you declared in the DetalhesViewController
class.
说到这一点,你应该声明该属性为 copy
,而不是 retain
。否则,你会发现自己持有别人的mutable数组,然后他们会修改它 - 更糟糕的mojo。
Speaking of which, you should declare that property as copy
, not retain
. Otherwise, you'll find yourself holding somebody else's mutable array, which they will then modify—more bad mojo.
这篇关于如何在视图控制器之间传递变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!