在Xcode中,类之间简单传递变量的方法

3
我正在尝试开发一个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]; 

}

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


1
添加您的代码,然后您可以得到帮助 - 没有人会为您编写应用程序。 - AnthonyBlake
我在考虑一个简单的空白应用程序,但好的,几分钟后就会到了。 - Shawn
@DrummerB:哈哈哈,我会保存这个链接! - Martin
3个回答

12

有几件事情需要做。取决于应用程序,你可以添加一个变量到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;
}

这将生成一个单例。如果你记得在两个视图控制器中导入了"StoreVars.h",那么你可以像这样访问共享数组;

[StoreVars sharedInstance].mySharedArray;
               ^

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

如果您不喜欢UINavigationController并希望使用segues,那么它会更容易,但在我看来可能会使您的应用程序变得“混乱”。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];
    }
}

来源:如何传递 prepareForSegue 中的对象

在提问此类问题之前,请先进行一些研究。阅读一些教程,自己尝试一下,然后提出与您真正想要了解的内容相关的问题。并非每天都有人愿意为您完成所有工作,但有时候您会很幸运,就像今天一样。

祝好。


谢谢你的解决方案,我正在使用storevars,它非常有效。 - Shawn

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

1
[segue destinationViewController]; FirstViewController *fv; fv.value = 10;类似这样的代码? - Shawn
你需要在 fv 赋值之前进行赋值操作,因此在 fv.value = 10; 之前,你需要将 fv 赋值为 [segue destinationViewController]; - John Smith

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接