RootViewController 中的方法不存储数组
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在这里缺少一些非常重要的基础知识。如果您在 SecondViewController 中分配一个新的 RootViewController,它与您用于创建 SecondViewController 的实例不是同一实例,因此它不会引用您要添加对象的数组。你试图做的事情不会成功。您必须在 SecondViewController 中为 RootViewController 创建一个 ivar,然后在第二个视图中访问它。像这样的事情:
你的 ivar 需要在 SecondViewController.h 中这样声明:
然后在 .m 中合成 然后
,你可以从 SecondViewController 中访问 ivar:
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:
Your ivar would need declared like this in the SecondViewController.h:
And then synthesized in the .m
Then, you can access the ivar from within your SecondViewController: