objective-c 如何在视图控制器之间传递变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/383101/
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
How do I pass variables between view controllers?
提问by
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.
我在使用 Xcode 时遇到了困难;出于某种原因,它不会让我将变量从一个视图控制器类传递到另一个视图控制器类。它应该可以工作,我基本上只是从我的其他课程中复制/粘贴(它适用于所有课程......除了这个课程)。我整晚都在处理它,尝试了我能想到的一切,但它仍然存在。
Here's the view controller class where I'm making the call:
这是我正在调用的视图控制器类:
ResultadosViewController.h:
结果视图控制器.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:
结果视图控制器.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];
}
}
And here's the other class i want to send the array into:
这是我想将数组发送到的另一个类:
DetalhesViewController.h:
详细视图控制器.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
I'm not sure if any of you would to see the .m file for this class; in that case, just ask.
我不确定你们是否会看到这个类的 .m 文件;在这种情况下,就问吧。
Thanks in advance, Hal
提前致谢,哈尔
PS: tried with other variables (other types too), cleansed/rebuilt, recreated xib file, you name it... i'm outta tricks :(
PS:尝试使用其他变量(也有其他类型),清理/重建,重新创建xib文件,你说出来......我已经失去了技巧:(
回答by Peter Hosey
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.
首先,不要使用->——这是直接的实例变量访问。它可能会起作用,但是您在它不知情的情况下更改了另一个对象的实例变量,这只是在自找麻烦。
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.
不,亚当·罗森菲尔德不是这个意思dvc->array_resultados;他的意思是resultadosControllerCell->array_resultados,这就是他所说的,他基于你所说的。
The correct solution is a blend of your original line and your revision of Adam's line:
正确的解决方案是您的原始行和您对亚当行的修订的混合:
dvc.array_resultados = [self array_resultados];
This goes through the property you declared in the DetalhesViewControllerclass.
这通过您在DetalhesViewController类中声明的属性。
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.
说到这里,您应该将该属性声明为copy,而不是retain。否则,您会发现自己持有其他人的可变数组,然后他们将对其进行修改——更糟糕的魔力。
回答by Matt Ball
This doesn't exaclty answer your question, but your memory management is a bit wonky. This line:
这并不能完全回答您的问题,但是您的内存管理有点不稳定。这一行:
[dvc dealloc];
should read like this:
应该是这样的:
[dvc release];
dvc = nil;
In Cocoa, you should never call deallocdirectly -- follow the retain/release/autoreleasepattern and things will work better and as intended.
在 Cocoa 中,你永远不应该dealloc直接调用——遵循retain/release/autorelease模式,事情会按预期工作得更好。
回答by Matt Ball
Ok i've had it; using a cheap trick to show the info:
好的,我已经拥有了;使用便宜的技巧来显示信息:
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];
Really hate using this, but i'm already a day late. I should have delivered it by yesterday.
真的很讨厌使用这个,但我已经晚了一天。我应该在昨天之前交付它。
Thanks for your help guys, if you happen to find the solution please let me know. You never know if it'll happen again :/
感谢您的帮助,如果您碰巧找到了解决方案,请告诉我。你永远不知道它是否会再次发生:/
Cheers!
干杯!
回答by Adam Rosenfield
Just a guess, but did you try using the arrow operator ->instead of the dot operator .?
只是猜测,但您是否尝试使用箭头运算符->而不是点运算符.?
resultadosControllerCell->array_resultados = [self array_resultados];
回答by RyanL
A more competent way of doing this is to separate the data from both the viewcontrollers into a model. You will have a separate class(NSObject) called ResultadoModel.h/m. This will be a singleton, so both classes can access the same instance.
一种更有效的方法是将来自两个视图控制器的数据分离到一个模型中。您将拥有一个名为 ResultadoModel.h/m 的单独类 (NSObject)。这将是一个单例,因此两个类都可以访问同一个实例。
You would access the array by doing something like this(in both vcs):
您可以通过执行以下操作(在两个 vcs 中)来访问数组:
[[[ResultadoModel sharedInstance] array_resultados] propertyOrMethod];
You can search how to create a singleton, it's very simple and very powerful.
您可以搜索如何创建单例,它非常简单且非常强大。
回答by Monica Chaturvedi
Follow these simple steps please, it should work.
请按照这些简单的步骤操作,它应该可以工作。
In your SecondViewController:
在你的 SecondViewController 中:
Create an initializer in the second View Controller. Ex:
-
(id)initWithResultsArray: (NSArray *) resultsArray;Create a variable for holding the array. Say
myResultsArray.Inside the
initWithResultsArraymethod, save the value ofresultsArraytomyResultsArray.Initialize the
SecondViewControllerusinginitWithResultsArrayinstead of justinit.Present your controller as usual, you will be able to work with
myResultsArray.
在第二个视图控制器中创建一个初始化程序。前任:
-
(id)initWithResultsArray: (NSArray *) resultsArray;创建一个变量来保存数组。说
myResultsArray。在
initWithResultsArray方法内部,保存resultsArrayto的值myResultsArray。初始化
SecondViewControllerusinginitWithResultsArray而不是 justinit。像往常一样展示您的控制器,您将能够使用
myResultsArray.
Hope this helps.
希望这可以帮助。
Monica ツ
莫妮卡ツ

