简单地在Xcode中的类之间传递变量 [英] Simple Passing of variables between classes in Xcode

查看:103
本文介绍了简单地在Xcode中的类之间传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个ios应用程序,但我困在类之间传递数据。
这是我的第二个应用程序。第一个是做白色一个全局类,但现在我需要
多个类。我尝试了很多教程,但没有工作或传递的值总是为零。有人可以给我写一个简单的应用程序来演示在IOS 5中传递变量。
没什么特别的,故事板whith 2视图控制器,一个变量。

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 .

感谢您的帮助。

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

}

这里是我的主视图中的代码,我需要发送indexPath.row或者我按下到下一个视图的单元格的索引

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

推荐答案

有几个事情要做。根据应用程序,您可以向AppDelegate类中添加一个变量,使其可以通过共享实例访问所有类。最常见的事情(我想)是做一个单身。为了工作,你可以创建一个类,例如StoreVars和一个返回对象的静态方法,这使得类全局。在方法中,你初始化所有的变量,就像你总是那样。

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.

@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;
}

这将使单例。如果你还记得在你的两个viewControllers中导入StoreVars.h,你可以像这样访问现在的共享数组;

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;

[StoreVars sharedInstance].mySharedArray;
               ^

这是一个返回StoreVars对象的方法。在StoreVars类中,您可以实现任何对象并在静态方法中初始化它。只要永远记住初始化它,或者,所有的对象将是0 / nil。

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.

如果你不是UINavigationController的粉丝,而宁愿使用segues,很容易,但可以使你的应用程序相当凌乱imo。有一个方法实现在UIViewController你应该重载:

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:

- (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:ios5如何传递prepareForSegue:一个对象

在提出这样的问题之前做一些研究。阅读一些教程,尝试自己,然后提出有关你真正寻找什么的问题。这不是每天人都想为你做所有的工作,但有时你是幸运的。喜欢今天。

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.

干杯。

这篇关于简单地在Xcode中的类之间传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆