在Objective-C iOS中的视图控制器的应用程序委托数组中添加对象



帮助我在Objective-C中的UIViewController的应用程序委托数组中添加对象,我想我做错了,在这方面帮助我

我想从视图控制器在应用程序委托数组中添加对象

在应用程序代表.h 中

@property (nonatomic, retain) NSMutableArray *sharedArray;

在 appdelegate.m 中

@implementation AppDelegate
@synthesize sharedArray;

内部didfinishlaunching

self.sharedArray = [[NSMutableArray alloc] init];

在视图控制器中

@interface ViewController () {
    UIApplication *appDelegate;


viewcontroller viewdidload内幕

appDelegate = [[UIApplication sharedApplication] delegate];
[appdelegate.sharedArray addObject:array];

你的代码很好,是正确的,但是你需要在附加对象之前初始化数组的内存

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    self. sharedArray = [NSMutableArray array];
    return YES;
}

最后调用该方法为

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
 [appDelegate.sharedArray addObject:array];

最后导入标题

#import "AppDelegate.h"

更改声明

UIApplication *appDelegate;

AppDelegate *appDelegate;

//

self.appDelegate =  (AppDelegate *)[[UIApplication sharedApplication] delegate];

在顶部你应该

#import "AppDelegate.h"

最新更新