ios 在 Xcode 中的类之间简单传递变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10649370/
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
Simple Passing of variables between classes in Xcode
提问by Shawn
I am trying to do an ios app but I am stuck at passing data between classes . This is my second app . The first one was done whit a global class , but now I need multiple classes . I tried many tutorials , but did not work or the passed value was always zero . Can someone please write me a simple app to demonstrate passing of variables in IOS 5 . Nothing special , storyboard whith 2 view controllers , one variable .
我正在尝试做一个 ios 应用程序,但我坚持在类之间传递数据。这是我的第二个应用程序。第一个是用一个全局类完成的,但现在我需要多个类。我尝试了很多教程,但没有奏效或传递的值始终为零。有人可以给我写一个简单的应用程序来演示 IOS 5 中变量的传递吗?没什么特别的,故事板有 2 个视图控制器,一个变量。
Thank you for your help .
感谢您的帮助 。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
FirstViewController *fv;
fv.value = indexPath.row;
NSLog(@"The current %d", fv.value);
FirstViewController *detail =[self.storyboard instantiateViewControllerWithIdentifier:@"Detail"];
[self.navigationController pushViewController:detail animated:YES];
}
here is the code from my main view and i need to send the indexPath.row or the index of the cell that i pressed to the next view
这是我的主视图中的代码,我需要将 indexPath.row 或我按下的单元格的索引发送到下一个视图
回答by Martol1ni
There are several things to do. Depending on the app, you could either add a variable to the AppDelegate class, making it availible to all classes through a shared instance. The most common thing (I think) is to make a singleton. For that to work, you can make a class, say StoreVars, and a static method that returns the object, which makes the class "global". Within the method, you initialize all your variables, like you always would. Then you can always reach them from wherever.
有几件事要做。根据应用程序,您可以向 AppDelegate 类添加一个变量,使其通过共享实例可用于所有类。最常见的事情(我认为)是制作一个单身人士。为此,您可以创建一个类,例如 StoreVars,以及一个返回对象的静态方法,这使类成为“全局”类。在该方法中,您可以像往常一样初始化所有变量。这样您就可以随时随地联系到他们。
@interface StoreVars : NSObject
@property (nonatomic) NSArray * mySharedArray;
+ (StoreVars*) sharedInstance;
@implementation StoreVars
@synthesize mySharedArray;
+ (StoreVars*) sharedInstance {
static StoreVars *myInstance = nil;
if (myInstance == nil) {
myInstance = [[[self class] alloc] init];
myInstance.mySharedArray = [NSArray arrayWithObject:@"Test"];
}
return myInstance;
}
This will make a singleton. If you remember to import "StoreVars.h" in your two viewControllers, you can access the now shared array like this;
这将成为一个单身人士。如果你记得在你的两个 viewController 中导入“StoreVars.h”,你可以像这样访问现在共享的数组;
[StoreVars sharedInstance].mySharedArray;
^
This is a method returning a StoreVars object. Within the StoreVars class, you can implement any object and initialize it in the static method. Just always remember to initialize it, or else, all your object will be 0/nil.
这是一个返回 StoreVars 对象的方法。在 StoreVars 类中,您可以实现任何对象并在静态方法中对其进行初始化。永远记住初始化它,否则,你所有的对象都将是 0/nil。
If you are not a fan of the UINavigationController and would rather use segues, it's a lot easier, but can make your app rather "messy" imo. There is a method implemented in UIViewController you should overload:
如果您不是 UINavigationController 的粉丝并且更愿意使用 segues,它会容易得多,但会使您的应用程序相当“凌乱”imo。在 UIViewController 中实现了一个方法,您应该重载:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
{
// Get reference to the destination view controller
YourViewController *vc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
[vc setMyObjectHere:object];
}
}
source: How to pass prepareForSegue: an object
Do some research before asking questions like this. Read some tutorials, and try out yourself, and then ask questions related to what you are really looking for. It's not everyday people want to do all the work for you, but sometimes you're lucky. Like today.
在提出这样的问题之前先做一些研究。阅读一些教程,自己尝试一下,然后提出与您真正想要的相关的问题。不是常人想为你做所有的工作,但有时你很幸运。好像今天。
Cheers.
干杯。
回答by John Smith
if you use segue between 2 controllers you must overload prepareToSegue method
如果在 2 个控制器之间使用 segue,则必须重载 prepareToSegue 方法
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// check if it's the good segue with the identifier
if([[segue identifier] isEqualToString:@"blablabla"])
{
// permit you to get the destination segue
[segue destinationViewController];
// then you can set what you want in your destination controller
}
}
回答by matm
The problem you face is quite perplexing for beginners. "Solving" it wrong way can result in learning a ton of bad habits.
Please have a look at Ole Begemann's excellent tutorial on Passing Data Between View Controllers- it's really worth reading.
您面临的问题对于初学者来说是相当令人困惑的。以错误的方式“解决”它会导致学习大量的坏习惯。
请看一下Ole Begemann的关于在视图控制器之间传递数据的优秀教程——它真的值得一读。