简体   繁体   中英

Referencing a ViewController in AppDelegate - Objective C

I am trying to get a NSMutableArray from another class. I am trying to reference that ViewController that contains the array. Lets say, a has the array I want. b is the class I am going to get that array in.

ah:

@property (strong, nonatomic) NSMutableArray *selectedCells;

am:

@synthesize selectedCells;

AppDelegate.h:

@property (strong, nonatomic) a *create_challengeDelegate;

AppDelegate.m:

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

Right here when I try to reference that ViewController I get an error saying:

Initializer element is not a compile-time constant

I assume it has something to do with it not being able to see the ViewController.

In my bm:

AppDelegate *app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
app.create_challengeDelegate.selectedCells

My issue is working with initializing the ViewController in the delegate.

Suggestions and thoughts on that?

My suggestion is that you create the selectedCells array in you AppDelegate and send it to A . Eg in:

AppDelegate.h

@property (nonatomic, strong) NSArray *selectedCells;

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.selectedCells = [NSMutableArray array];

    A *viewA = [[a alloc] initWithNibName:@"a" bundle:nil selectedCells:self.selectedCells];

    [...]
}

ah

@property (nonatomic, strong) NSMutableArray *selectedCells;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil selectedCells:(NSMutableArray*)cells;

am

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil selectedCells:(NSMutableArray*)cells
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.selectedCells = cells;
    }
    return self;
}

bm

AppDelegate *app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
NSMutableArray *selectedCells = app.selectedCells;

This way, whenever you make changes to selectedCells it will keep it in the array you send there since selectedCells is a refference to an object which is created in your AppDelegate .

Otherwise, what you are trying to do is access a view which might no longer be available in the memory since the view might have been deallocated. Also, you create the instance of a but its not set to the AppDelegate 's instance of a , its a seperate instance.

Instead of

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

You should have

@synthesize create_challengeDelegate;
self.create_challengeDelegate = [[a alloc] init];

I still strongly recommend you do not access your view this way though.

Ps @synthesize is no longer necessary.

Edit
This is a better solution.

Selection.h

/**
 *  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

Selection.m

#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];
}

Now you can assess your shared singeton in A and/or B by accessing:

[[Selection sharedInstance] addObject:myObject];

[[Selection sharedInstance] removeObject:myObject];

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

Is this line inside a method:

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

If not that would probably explain your issue. If that's not the problem it might be helpful to post more complete code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM