Objective c Xcode中类之间变量的简单传递

Objective c Xcode中类之间变量的简单传递,objective-c,ios,xcode,class,Objective C,Ios,Xcode,Class,我试图做一个ios应用程序,但我被困在类之间传递数据。 这是我的第二个应用程序。第一个是通过一个全局类完成的,但现在我需要 多个类。我尝试了很多教程,但都不起作用,或者传递的值始终为零。有人能给我写一个简单的应用程序来演示IOS 5中变量的传递吗。 没有什么特别的,故事板有两个视图控制器,一个变量 谢谢你的帮助 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

我试图做一个ios应用程序,但我被困在类之间传递数据。 这是我的第二个应用程序。第一个是通过一个全局类完成的,但现在我需要 多个类。我尝试了很多教程,但都不起作用,或者传递的值始终为零。有人能给我写一个简单的应用程序来演示IOS 5中变量的传递吗。 没有什么特别的,故事板有两个视图控制器,一个变量

谢谢你的帮助

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

}

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

如果在两个控制器之间使用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
}
}

有几件事要做。根据应用程序的不同,您可以向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;
}
这将成为一个单身汉。如果您记得在两个ViewController中导入“StoreVars.h”,您可以像这样访问现在共享的数组

[StoreVars sharedInstance].mySharedArray;
               ^
这是一个返回StoreVars对象的方法。在StoreVars类中,您可以实现任何对象并在静态方法中对其进行初始化。只要记住初始化它,否则,所有对象都将是0/nil

如果您不喜欢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];
    }
}
资料来源:

在问这样的问题之前做一些调查。阅读一些教程,尝试一下自己,然后问一些与你真正想要的相关的问题。不是每天都有人想为你做所有的工作,但有时候你很幸运。就像今天一样


干杯。

对于初学者来说,你面临的问题相当复杂。以错误的方式“解决”它会导致学习大量的坏习惯。

请看一看这本书的优秀教程,它非常值得一读。

添加代码,你会得到帮助-没有人会为你编写应用程序。我想用一个简单的空白应用程序,但没问题,马上就可以了minutes@DrummerB:哈哈,我保留这个链接![segue destinationViewController];FirstViewController*fv;fv.值=10;类似的事情?你需要在fv.value=10之前分配你的fv;您需要fv=[segue destinationViewController];thnx对于解决方案,我使用的是storevars,它的工作方式很有魅力