无法从SecondaryView添加到NSMutableArray

时间:2010-06-12 23:09:08

标签: objective-c iphone-sdk-3.0 nsmutablearray

我已经搜索过并且仍然没有找到具体的答案。

简介: 我有一个应用程序,我在AppDelegate中声明一个NSMutableArray,以便在应用程序加载时进行合成。 (以下代码)。我有一个SecondaryViewController调用这个函数,我输出一个字符串让我知道数组大小是什么。每次运行它时,它都会执行但不会向数组中添加任何对象。我该如何解决这个问题?

AppDelegate.h文件

#import <UIKit/UIKit.h>

@class arrayTestViewController;

@interface arrayTestAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    arrayTestViewController *viewController;

    NSMutableArray *myArray3;
}

@property (nonatomic, retain) NSMutableArray *myArray3;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet arrayTestViewController *viewController;

-(void)addToArray3;

@end

AppDelegate.m文件

#import "arrayTestAppDelegate.h"
#import "arrayTestViewController.h"

@implementation arrayTestAppDelegate

@synthesize window;
@synthesize viewController;
@synthesize myArray3;

#pragma mark -
#pragma mark Application lifecycle



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    myArray3 = [[NSMutableArray alloc] init];


    // Override point for customization after application launch.

    // Add the view controller's view to the window and display.
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

    return YES;
}

-(void)addToArray3{

    NSLog(@"Array Count: %d", [myArray3 count]);
    [myArray3 addObject:@"Test"];
    NSLog(@"Array triggered from SecondViewController");
    NSLog(@"Array Count: %d", [myArray3 count]);


}

SecondViewController.m文件

#import "SecondViewController.h"
#import "arrayTestAppDelegate.h"

@implementation SecondViewController



-(IBAction)addToArray{
    arrayTestAppDelegate *object = [[arrayTestAppDelegate alloc] init];
    [object addToArray3];
}

EDIT ***

好的,这就是我的尝试:

arrayTestViewController.h

#import <UIKit/UIKit.h>
#import "SecondViewController.h"

@interface arrayTestViewController : UIViewController {
    NSMutableArray *myArray;
    @private NSMutableArray *myArray2;
}

-(IBAction)showArray;
-(IBAction)switchViews;
-(IBAction)addToArray;


@end

arrayTestViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    myArray2 = [[NSMutableArray alloc] init];
    SecondViewController.myArray2 = myArray2;
    //ERRORS:Accessing unknown 'setMyArray2:" class method.
    //object cannot be set - either readonly property or no setter found
}

-(IBAction)switchViews{
    SecondViewController *screen = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
    screen.myArray2 = self.myArray2;
    //ERROR:Accessing unknown 'myArray2' getter method.

    screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:screen animated:YES];
    [screen release];
}

SecondViewController.h

#import <UIKit/UIKit.h>
#import "arrayTestViewController.h"


@interface SecondViewController : UIViewController{
    NSMutableArray *myArray2;
}

@property (nonatomic, retain) NSMutableArray *myArray2;

-(IBAction)addToArray;
-(IBAction)switchBack;

@end

SecondViewController.m

#import "SecondViewController.h"
#import "arrayTestViewController.h"

@implementation SecondViewController
@synthesize myArray2;


-(IBAction)addToArray{  
    arrayTestViewController *object = [[arrayTestViewController alloc] init];
    [object addToArray2];

}

它给了我三个错误(在代码中也有注释):

访问未知的'setMyArray2:“类方法。无法设置对象 - 无论是readonly属性还是找不到setter。访问未知的'myArray2'getter方法。


我最初在SecondViewController中尝试了这个,但结果相同。

感谢您关于不让他们留在代表中的建议:)

我如何解决这个问题,因为我更改它以便在RootViewController中初始化Array,然后从SecondViewController调用该函数?

1 个答案:

答案 0 :(得分:3)

-(IBAction)addToArray{
    arrayTestAppDelegate *object = [[arrayTestAppDelegate alloc] init];
    [object addToArray3];
}

上面的代码错了。您不应该alloc在视图控制器中使用应用程序委托。从来没有。永远。 (此外,每次泄漏记忆,因为您没有将alloc / initrelease配对。但我离题了......)

如果要从视图控制器访问应用程序委托,可以这样做:

arrayTestAppDelegate* delegate = (arrayTestAppDelegate*)[[UIApplication sharedApplication] delegate];
 [delegate addToArray];

虽然这回答了您的问题,但强烈敦促您不要将您的应用程序委托用作全局变量的持有者。虽然在应用程序委托中创建顶级数组并不“坏”,但在创建每个视图控制器时,应该将这些对象传递给视图控制器层次结构。从某种意义上来说,这是更清洁的,因为你没有在远处进行任何动作。 (单打单身!)

(P.S。在Cocoa中,惯例是类名以大写字母开头 - arrayTestAppDelegate应重命名为ArrayTestAppDelegate

修改

  

我如何解决这个问题,因为我更改它以便在RootViewController中初始化Array,然后从SecondViewController调用该函数?

首先,拥有对象的对象应该是负责初始化对象的对象。不要将您的数组放在您的应用程序委托中,然后在其他地方初始化它。这是“远距离行动”的另一种形式,以后很难阅读和调试。

其次,回答您的问题确实需要有关您的控制器层次结构中“RootViewController”和“SecondViewController”的存在方式的更多信息。但是我可以给你一个关于沿着线传递对象(比如你的数组)的一般答案。

(我假设您的RootViewController是您应用中的顶级视图控制器,而SecondViewControllerRootViewController显示的视图控制器。根据名称判断,这是一个安全的赌注。)

ArrayTestAppDelegate.h (注意大小写)可能如下所示:

...
@interface ArrayTestAppDelegate : NSObject <UIApplicationDelegate>
{
    ...
@private
    NSMutableArray* theArray;
    ...
}
...

请注意缺少@property

ArrayTestAppDelegate.m 可能如下所示:

...
@implementation ArrayTestAppDelegate
...
- (void)applicationDidFinishLaunching:(UIApplication*)application
{
    ...
    theArray = [[NSMutableArray alloc] init];
    ...
    rootViewController.theArray = theArray;

    [window addSubview:rootViewController.view];
    [window makeKeyAndVisible];
}
...
- (void)dealloc
{
    [theArray release];
}
...
@end

<强> RootViewController.h

...
@interface RootViewController : UITabBarController
{
    ...
    NSMutableArray* theArray;
    ...
}
...
@property (nonatomic, retain) NSMutableArray* theArray;
...

<强> RootViewController.m

@implementation RootViewController
...
@synthesize theArray;
...
- (void)dealloc
{
    [theArray release];
}
...
- (void)showSecondViewController
{
    SecondViewController* secondViewController = [[SecondViewController alloc] initWithNib:@"SecondView"];
    secondViewController.theArray = self.theArray;
    // Show secondViewController here -- modally, using nav controller stack, etc.
    [secondViewController release];
}
...
@end

等等。我向读者留下了为theArray实施@property SecondViewController的练习(提示:很像RootViewController