我想从另一个班级获得NSMutableArray
。我试图引用包含该数组的ViewController。可以说,a
有我想要的数组。 b
是我将要获得该数组的类。
A.H:
@property (strong, nonatomic) NSMutableArray *selectedCells;
A.M:
@synthesize selectedCells;
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。
对此的建议和想法?
答案 0 :(得分:1)
我的建议是,您在selectedCells
中创建AppDelegate
数组并将其发送给A
。例如。在:
<强> 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];
[...]
}
<强> A.H 强>
@property (nonatomic, strong) NSMutableArray *selectedCells;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil selectedCells:(NSMutableArray*)cells;
<强> A.M 强>
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil selectedCells:(NSMutableArray*)cells
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.selectedCells = cells;
}
return self;
}
<强> b.m 强>
AppDelegate *app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
NSMutableArray *selectedCells = app.selectedCells;
这样,每当您对selectedCells
进行更改时,它都会将其保留在您发送到的数组中,因为selectedCells
是对AppDelegate
中创建的对象的引用。
否则,您尝试执行的操作是访问内存中可能不再可用的视图,因为视图可能已被取消分配。
此外,您创建了a
的实例,但未将其设置为AppDelegate
的{{1}}实例,它是一个单独的实例。
而不是
a
你应该
@synthesize create_challengeDelegate;
a *create_challengeDelegate = [[a alloc]init];
我仍然强烈建议你不要这样访问你的观点。
P.S。 <{1}}已不再需要。
<强> 修改 强>
这是一个更好的解决方案。
Selection.h
@synthesize create_challengeDelegate;
self.create_challengeDelegate = [[a alloc] init];
Selection.m
@synthesize
现在,您可以访问以下内容,在/**
* 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];
}
评估您的共享singeton:
A
答案 1 :(得分:1)
该行是否在方法中:
a *create_challengeDelegate = [[a alloc]init];
如果不是那可能会解释你的问题。如果这不是问题,那么发布更完整的代码可能会有所帮助。