【问题标题】:Adding objects in array of app delegate from view controller in Objective-C iOS从Objective-C iOS中的视图控制器添加应用程序委托数组中的对象
【发布时间】:2018-04-11 10:54:15
【问题描述】:

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

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

在 appdelegate.h 中

@property (nonatomic, retain) NSMutableArray *sharedArray;

在 appdelegate.m 中

@implementation AppDelegate
@synthesize sharedArray;

在didfinishlaunching内

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

在视图控制器中

@interface ViewController () {

    UIApplication *appDelegate;


viewdidload 内部 viewcontroller

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

【问题讨论】:

  • 你能解释一下你想做什么吗?
  • 您的问题是什么?反正你不需要在ViewController中声明appDelegate
  • 我想在视图控制器的应用委托数组中添加对象
  • @iOSDeveloper 查看 Anbu 的回答。
  • 在你想使用的地方导入 AppDelegate

标签: ios objective-c nsarray


【解决方案1】:

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

- (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"

【讨论】:

  • 我添加了 self.sharedArray = [[NSMutableArray alloc] init];
  • @RealmOfFire- 兄弟,它有效,我们需要提问者的确认
  • @Anbu.karthik 支持您的回答,但我忘记导入应用委托
【解决方案2】:

变更声明

UIApplication *appDelegate;

到

AppDelegate *appDelegate;

//

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

//你应该在上面

#import "AppDelegate.h"

【讨论】:

  • Unknown type name 'AppDelegate' 执行此操作时会出现此错误
  • 快乐编码 ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-09
  • 1970-01-01
  • 2015-08-22
  • 2011-12-29
  • 2011-12-27
  • 1970-01-01
相关资源
最近更新 更多