简体   繁体   English

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

[英]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.

相关问题 ios从第二个视图控制器向第一个视图控制器发送Objective-c数据发送 - ios Delegate Objective-c data sending from 2nd view controller to 1st view controller Objective-C iOS应用程序未在对象数组中存储数据 - Objective-C iOS app not storing data in array of objects 如何从iOS Objective-C中的控制器重绘自定义视图 - how to redraw custom view from the controller in ios objective-c 我需要从SpriteKit GameViewController过渡到iOS应用中的常规View Controller(Objective-C) - I need to transition from a SpriteKit GameViewController to a regular View Controller in an iOS app (Objective-C) App代表中的iOS 6 Objective-C Post Splash屏幕 - iOS 6 Objective-C Post Splash Screen in App Delegate 从委托视图向委托视图发送数据[Objective-C] - Sending Data From Delegate View To Delegated View [Objective-C] 从目标C / iOS中的另一个视图控制器访问C数组 - Accessing C array from another view controller in objective C/iOS 从详细视图控制器访问数组到Objective-C中的主视图控制器 - Accessing array from a detail view controller into master view controller in Objective-C iOS更改视图控制器来自应用程序委托? - iOS change view controller from the app delegate? 如何在另一个视图控制器中控制对象? (Xcode:objective-c) - How to control objects in another view controller? (Xcode:objective-c)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM