Re: Connecting a button to MyView zeros integers
Re: Connecting a button to MyView zeros integers
- Subject: Re: Connecting a button to MyView zeros integers
- From: N!K <email@hidden>
- Date: Sun, 15 Feb 2015 22:50:01 -0800
On Feb 13, 2015, at 8:42 AM, David Duncan <email@hidden> wrote:
> Since you elided your header file, this begs a question – is this iOS or OS X?
This is OSX.
I was following Apple’s example, which control-drags from a button to the .m file. The .h file isn’t used. I supplied the shortest possible file to avoid confusion.
>
> If I’m not mistaken, subclasses of NSView do not get -initWithCoder:, but rather -initWithFrame:, which begs the question of have you confirmed that your -initWithCoder: method is being called?
Yes. With a breakpoint.
InitWithCoder is called because Runtime Behavior, Instantiation, Prefer Coder is checked in xib’s inspector.
There was quite a discussion about why it works this way 12/23/14 - 12/30/14, in response to my request for help, “initWithFrame fails” Roland King nailed it in his email 12/26/2014.
Nick
>
>> On Feb 6, 2015, at 6:22 PM, N!K <email@hidden> wrote:
>>
>>
>> I would like to connect a button to MyView class, but Xcode 6.1.1 only allows control-dragging a button to AppDelegate to create an IBAction. I have not encountered this previously. Looking for a workaround, I found this recommendation in a couple of Stack Overflow and other web pages as well as a YouTube video. It enables the button to work, but unfortunately it zeros all the integers in MyView.
>>
>> The recommendation is:
>> 1. Drag an empty Object from the IB library to the column of blue icons.
>> 2. Set its class to MyView.
>> 3. Control-drag from the button to MyView.m
>> 4. Fill in the name (“act”) in the popup.
>> This puts the IBAction template into MyView, ready to fill in.
>>
>> #import "MyView.h"
>>
>> @implementation MyView
>>
>> - (id)initWithCoder:(NSCoder *)aDecoder
>> {
>> self = [super initWithCoder:aDecoder];
>> if (self) {
>> iii=1000;
>> k=99;
>> }
>> return self;
>> }
>>
>> - (IBAction)act:(id)sender {
>> iii=iii+1;
>> NSLog(@" iba i= %i",iii);
>> }
>>
>>
>> In MyView.m, iii=1000 is initialized in initWithCoder. At the breakpoint after IBAction, iii is seen in both places to have the value 1, not 1001, by hovering. It was zeroed and then incremented after clicking on Button in the View. Similarly, k is initialized to 99 and then zeroed. Both are ivars in MyView.h.
>>
>> Clearly, zeroing all the integers is not acceptable. Can this approach be saved? Having the IBAction in MyView is desirable for directly relating its functions to the rest of MyView, rather than indirectly from AppDelegate. On the other hand, Xcode may have very good reasons – unknown to me – for restricting IBAction to AppDelegate. Maybe timing?
>>
>> Thanks in advance,
>>
>> Nick
>> _______________________________________________
>>
>> Cocoa-dev mailing list (email@hidden)
>>
>> 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:
>>
>> This email sent to email@hidden
>
> --
> David Duncan
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden