Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在AppDelegate中引用ViewController-目标C_Ios_Objective C - Fatal编程技术网

Ios 在AppDelegate中引用ViewController-目标C

Ios 在AppDelegate中引用ViewController-目标C,ios,objective-c,Ios,Objective C,我正在尝试从另一个类获取NSMutableArray。我正在尝试引用包含阵列的ViewController。比方说,a有我想要的数组。b是我将要得到数组的类 a、 h: a、 m: AppDelegate.h: @property (strong, nonatomic) a *create_challengeDelegate; AppDelegate.m: @synthesize create_challengeDelegate; a *create_challengeDelegate = [

我正在尝试从另一个类获取NSMutableArray。我正在尝试引用包含阵列的ViewController。比方说,a有我想要的数组。b是我将要得到数组的类

a、 h:

a、 m:

AppDelegate.h:

@property (strong, nonatomic) a *create_challengeDelegate;
AppDelegate.m:

@synthesize create_challengeDelegate;
a *create_challengeDelegate = [[a alloc]init];
就在这里,当我尝试引用该ViewController时,我得到一个错误,错误是:

Initializer element is not a compile-time constant
我想这与无法看到ViewController有关

在我的b.m

AppDelegate *app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
app.create_challengeDelegate.selectedCells
我的问题是如何在代理中初始化ViewController


关于这方面的建议和想法?

我的建议是,您在AppDelegate中创建selectedCells数组,并将其发送给A。例如,在:

AppDelegate.h

AppDelegate.m

a、 h

a、 m

b、 m

这样,无论何时更改selectedCells,它都会将其保留在发送到那里的数组中,因为selectedCells是对AppDelegate中创建的对象的引用

否则,您将尝试访问内存中可能不再可用的视图,因为该视图可能已被解除分配。 另外,您创建了的实例,但它没有设置为AppDelegate的实例,而是一个单独的实例

而不是

@synthesize create_challengeDelegate;
a *create_challengeDelegate = [[a alloc]init];
你应该

@synthesize create_challengeDelegate;
self.create_challengeDelegate = [[a alloc] init];
我仍然强烈建议您不要以这种方式访问视图

附言:不再需要合成

编辑 这是一个更好的解决方案

选择.h

选择

现在,您可以通过访问以下内容来评估A和/或B中的共享Singleton:

[[Selection sharedInstance] addObject:myObject];

[[Selection sharedInstance] removeObject:myObject];

NSArray *array = [[Selection sharedInstance] getSelections];

此行是否在方法中:

a *create_challengeDelegate = [[a alloc]init];

如果不是的话,这可能会解释你的问题。如果这不是问题所在,那么发布更完整的代码可能会有所帮助

希望您没有忘记在AppDelegate中导入a.h。h@faisalali我不是,我在appdelegate.hWhere中包含import,以及当xcode、compile、runtime出现错误时?@hermannKlecker,请原谅我发布视图控制器的init方法时出现的错误。不是,我把它放在didFinishLaunchingWithOptionsselectedCells中,它在视图控制器中使用,从它的名称来看,它意味着它可能与一个表视图相关联,只是术语selected本身意味着与用户交互有关。因此,我不同意将其放入app delegate,首先app delegate通常是一个垃圾场,但第二,如果selectedCells只是一个GUI显示相关的数据结构,那么在我的拙见中,它应该保存在视图控制器中,我对一般的数据结构和GUI显示相关的数据结构进行了区分。对于me@Piepants你的论点是正确的,我同意你的观点。最好使用共享单例来保存共享数据。然而,通过AppDelegate从b访问VC a是比我上面提到的更糟糕的解决方案。尤其是在使用ARC时。@DavidBiga我用更好的解决方案更新了我的答案,然后将其添加到您的AppDelegate中。
@property (nonatomic, strong) NSMutableArray *selectedCells;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil selectedCells:(NSMutableArray*)cells;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil selectedCells:(NSMutableArray*)cells
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.selectedCells = cells;
    }
    return self;
}
AppDelegate *app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
NSMutableArray *selectedCells = app.selectedCells;
@synthesize create_challengeDelegate;
a *create_challengeDelegate = [[a alloc]init];
@synthesize create_challengeDelegate;
self.create_challengeDelegate = [[a alloc] init];
/**
 *  The entity for selections
 */
@interface Selection : NSObject

/**
 *  The Shared Instance
 *
 *  @return Selection The instance
 */
+ (Selection *)sharedInstance;

/**
 *  Add an item to the selections
 *
 *  @param object id The object to add
 */
- (void)addSelection:(id)object;

/**
 *  Remove an item from the selections
 *
 *  @param object id The object to remove
 */
- (void)removeObject:(id)object;

/**
 *  Get the selections
 *
 *  @return NSArray The array with the current selection objects
 */
- (NSArray *)getSelections;

@end
#import "Selection.h"

@interface Selection ()

@property (nonatomic, strong) NSMutableArray *selections;

@end

@implementation Selection

#pragma mark - Public methods

+ (Selection *)sharedInstance
{
    static Selection *sharedInstance = nil;

    @synchronized(self) {
        if (sharedInstance == nil) {
            sharedInstance = [[self alloc] init];
        }
    }

    return sharedInstance;
}

#pragma mark - Private methods

- (id)init
{
    if (self = [super init])
    {
        self.selections = [NSMutableArray array];
    }
    return self;
}

#pragma mark - Manage selection

- (void)addSelection:(id)object
{
    [self.selections addObject:object];
}

- (void)removeObject:(id)object
{
    [self.selections removeOjbect:object];
}

- (NSArray *)getSelections
{
    return [NSArray arrayWithArray:self.selections];
}
[[Selection sharedInstance] addObject:myObject];

[[Selection sharedInstance] removeObject:myObject];

NSArray *array = [[Selection sharedInstance] getSelections];
a *create_challengeDelegate = [[a alloc]init];