RootViewController 中的方法不存储数组

发布于 2024-09-05 19:23:00 字数 1384 浏览 1 评论 0原文

我在 RootViewController 中初始化了一个数组,并有一个将对象添加到数组的方法。我在 SecondViewController 中创建了一个 RootViewController 对象。该方法运行(输出一条消息),但它没有向数组添加任何内容,并且数组看起来是空的。代码如下,有什么建议吗?

RootViewController.h

#import "RootViewController.h"
#import "SecondViewController.h"

@implementation RootViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    myArray2 = [[NSMutableArray alloc] init];

    NSLog(@"View was loaded");
}
-(void)addToArray2{
    NSLog(@"Array triggered from SecondViewController");
    [myArray2 addObject:@"Test"];
    [self showArray2];
}

-(void)showArray2{
    NSLog(@"Array Count: %d", [myArray2 count]);
}
-(IBAction)switchViews{
    SecondViewController *screen = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
    screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:screen animated:YES];
    [screen release];
}

SecondViewController.m

#import "SecondViewController.h"
#import "RootViewController.h"

@implementation SecondViewController

-(IBAction)addToArray{

    RootViewController *object = [[RootViewController alloc] init];
    [object addToArray2];

}
-(IBAction)switchBack{
    [self dismissModalViewControllerAnimated:YES];
}

编辑*************

使用 Matt 的代码,我收到以下错误:

“在 'RootViewController' 之前预期的说明符限定符列表”

I have an array initialized in my RootViewController and a method that addsObjects to an array. I created a RootViewController object in my SecondViewController. The method runs (outputs a message) but it doesn't add anything to the array, and the array seems empty. Code is below, any suggestions?

RootViewController.h

#import "RootViewController.h"
#import "SecondViewController.h"

@implementation RootViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    myArray2 = [[NSMutableArray alloc] init];

    NSLog(@"View was loaded");
}
-(void)addToArray2{
    NSLog(@"Array triggered from SecondViewController");
    [myArray2 addObject:@"Test"];
    [self showArray2];
}

-(void)showArray2{
    NSLog(@"Array Count: %d", [myArray2 count]);
}
-(IBAction)switchViews{
    SecondViewController *screen = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
    screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:screen animated:YES];
    [screen release];
}

SecondViewController.m

#import "SecondViewController.h"
#import "RootViewController.h"

@implementation SecondViewController

-(IBAction)addToArray{

    RootViewController *object = [[RootViewController alloc] init];
    [object addToArray2];

}
-(IBAction)switchBack{
    [self dismissModalViewControllerAnimated:YES];
}

EDIT*************

With Matt's code I got the following error:

" expected specifier-qualifier-list before 'RootViewController' "

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可爱咩 2024-09-12 19:23:00

您在这里缺少一些非常重要的基础知识。如果您在 SecondViewController 中分配一个新的 RootViewController,它与您用于创建 SecondViewController 的实例不是同一实例,因此它不会引用您要添加对象的数组。你试图做的事情不会成功。您必须在 SecondViewController 中为 RootViewController 创建一个 ivar,然后在第二个视图中访问它。像这样的事情:

-(IBAction)switchViews{
    SecondViewController *screen = [[SecondViewController alloc] 
                                       initWithNibName:nil bundle:nil];
    screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [screen setRootViewController:self];
    [self presentModalViewController:screen animated:YES];
    [screen release];
}

你的 ivar 需要在 SecondViewController.h 中这样声明:

@property (nonatomic, retain) RootViewController *rootViewController;

然后在 .m 中合成 然后

,你可以从 SecondViewController 中访问 ivar:

-(IBAction)addToArray{
    [[self rootViewController] addToArray2];
}

You are missing some very essential fundamentals here. If you allocate a new RootViewController in your SecondViewController, it is not the same instance as the one you used to create your SecondViewController so it will not have a reference to the array you are adding objects to. What you are trying to do won't work. You must create an ivar in your SecondViewController for your RootViewController and then access it inside second view. Something like this:

-(IBAction)switchViews{
    SecondViewController *screen = [[SecondViewController alloc] 
                                       initWithNibName:nil bundle:nil];
    screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [screen setRootViewController:self];
    [self presentModalViewController:screen animated:YES];
    [screen release];
}

Your ivar would need declared like this in the SecondViewController.h:

@property (nonatomic, retain) RootViewController *rootViewController;

And then synthesized in the .m

Then, you can access the ivar from within your SecondViewController:

-(IBAction)addToArray{
    [[self rootViewController] addToArray2];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文