[英]Adding objects in array of app delegate from view controller in Objective-C iOS
Help me in adding objects in array of app delegate from UIViewController in Objective-C I guess I am doing mistake, help me in this regard 帮助我在Objective-C中从UIViewController的应用程序委托数组中添加对象我想我做错了,在这方面帮助我
I want to add objects in array of app delegate from view controller 我想从视图控制器的应用程序委托数组中添加对象
in appdelegate.h 在appdelegate.h中
@property (nonatomic, retain) NSMutableArray *sharedArray;
in appdelegate.m 在appdelegate.m中
@implementation AppDelegate
@synthesize sharedArray;
inside didfinishlaunching
内部
didfinishlaunching
self.sharedArray = [[NSMutableArray alloc] init];
in ViewController 在ViewController中
@interface ViewController () {
UIApplication *appDelegate;
Inside viewdidload
of viewcontroller
viewcontroller
内部viewdidload
appDelegate = [[UIApplication sharedApplication] delegate];
[appdelegate.sharedArray addObject:array];
Your code is fine and correct, but you need to initialize the memory of your array before append the object 您的代码是正确无误的,但是在追加对象之前,您需要初始化数组的内存
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self. sharedArray = [NSMutableArray array];
return YES;
}
finally call the method as 最终将方法调用为
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.sharedArray addObject:array];
finally import the header 最后导入标题
#import "AppDelegate.h"
Change declaration of 变更声明
UIApplication *appDelegate;
to 至
AppDelegate *appDelegate;
// //
self.appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
// on top you should //最重要的是
#import "AppDelegate.h"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.