Hallo Graham,

the init-method of the DelegateClass is running. This is because I have a class 
object in the Document.xib file. But nevertheless I changed the init-method 
into initWithFrame with the same result. Then I changed it into initWithCoder 
and the result is not an array with (null) but an array without content.

My DocumentProtocol is only to send the array created in the document class to 
the DelegateClass. That’s all. And this part of code works fine. The 
DelegateClass gets the array created in the document class and if I press the 
button the button-action calls the showArray-method and the array will be 
printed out correctly. But only if I call the showArray method from within the 
drawRect-method, it will be printed out (null) or empty.

Yes I just need one array, but I have to work with the arrays values in another 
class. Because I don’t wanna create a global variable I send the array via a 
protocol.

There’s surely another way to work with the document array in the DelegateClass 
but I really want to understand, how it can be that an array has contend if the 
method which only prints the array out is called from a button-action and is 
(null) or empty if it’s called from drawRect.

I’m a very newbie and I think that it is important for every other app to 
understand this.

Many Thanks,
Raimond


> Am 08.09.2016 um 14:35 schrieb Graham Cox <graham....@bigpond.com>:
> 
> 
>> On 8 Sep 2016, at 9:27 PM, Raimond Hettrich <sa...@onlinehome.de> wrote:
>> 
>> @interface DelegateClass : NSView <DocumentProtocol> {
>>   NSMutableArray *delegateArray;
>> 
>> 
>>   __weak IBOutlet Document *documentOutlet;
>> }
>> 
>> @end
>> 
>> 
>> //
>> //  DelegateClass.m
>> //  Test
>> //
>> 
>> #import "DelegateClass.h"
>> 
>> @implementation DelegateClass
>> 
>> - (void)drawRect:(NSRect)dirtyRect {
>>   [super drawRect:dirtyRect];
>> 
>> 
>>   [self showArray];
>> }
>> 
>> - (id)init {
>>   self = [super init];
>>   if (self) {
>>       delegateArray = [[NSMutableArray alloc]init];
>>   }
>>   return self;
>> }
>> 
> 
> 
> The -init method of DelegateClass will never run. It’s not the designated 
> initializer for a NSView (-initWithFrame: is, but it could also be 
> -initWithCoder: depending on the setting in the nib), so your array will 
> never be created. That’s probably why it’s nil in -drawRect.
> 
> I don’t really understand what your ‘document protocol’ is doing, but I also 
> notice that you craete a different array in the Document class. Wouldn’t you 
> just want an array in one place, not two different ones?
> 
> —Graham
> 
> 


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to