• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to deal with a MenuItem with both a binded state property and an action method
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to deal with a MenuItem with both a binded state property and an action method


  • Subject: Re: How to deal with a MenuItem with both a binded state property and an action method
  • From: "Joan Lluch (casa)" <email@hidden>
  • Date: Sat, 28 Jun 2008 09:41:25 +0200


El 28/06/2008, a las 0:23, Keary Suska escribió:

6/27/08 4:06 PM, also sprach email@hidden:

I am struck in what should be a simple task so I would appreciate any
help.

I have a menu item which I want to show a default on or off state, so
I set up a binding in IB to a BOOL "monitor" property in my
controller. This works ok and both the check mark in the menu item and
the property are kept in sync whatever I change.


However, the menu item I am talking about is intended to trigger an
action that can fail, so I want to keep its ultimate state unchanged
in some cases. For this purpose I added an action method that checks
the original state of the menuState property and resets it accordingly
depending on the success status of the performed accion.


The code looks as follows


- (BOOL)menuState { return menuState ; }

- (void)setMenuState:(BOOL)value
    {
     NSLog(@"setMenuState (%d) called",value);
     menuState = value ;
    }


- (IBAction) myAction:(id)sender {

    do something...

    if ( fail ) [self setMenuState:NO] ;
   }

I observed that when the user clicks on the menu item the action
method is called before any change is made to menuState, but then
setMenuState may be called twice: one from myAction (if it fails) and
one from the frameworks. The problem here is that after the second
call both menuState and the menuItem no longer shows the intended state.

When using bindings always stick to MVC--i.e., don't touch the menu. If you
have to change the state, change the "monitor" value to reflect the correct
state. IN any case, you don't say how the menuState property enters into the
picture, vs the "monitor" property.




In fact "monitor" is meant to be the same as "menuState", Sorry, it was a typo since I was trying to use more understandable names for the dev-list. In the real app I am using "monitor" as the name of the variable but I thought "menuState" would be more a appropriate name for discussion here. So let's forget "monitor" ;) "menuState" is the model property that is binded to the menuItem value.


As you suggest, I actually do stick to MVC and KVC, so I only change the menuItem state through the model "menuState" property as shown in the "myAction" method.

The problem remains because when the user selects the menuItem the following happens:

FIRST- myAction is executed (possibly setting menuState to an appropiate value)
SECOND - setMenuState is executed with a value contrary to the last one, so if I had set it to NO in myAction, it is called now with YES, destroying completely the intended behaviour. The menu item then shows the wrong state in the running app.


I thought that I could get rid of the myAction method and observe the menuState property instead, but the whole problem still would remain because I still want to be able to set "menuState" to an appropriate state while observing it. Finally I could use some flag variable to tell me whether the setMenuState call comes from the menuItem click or from my own code, but that seems rather weird to me and I believe that there should be a cleaner way.

Any help is appreciated.

Joan Lluch


_______________________________________________

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


  • Follow-Ups:
    • Re: How to deal with a MenuItem with both a binded state property and an action method
      • From: Ken Thomases <email@hidden>
References: 
 >Re: How to deal with a MenuItem with both a binded state property and an action method (From: Keary Suska <email@hidden>)

  • Prev by Date: Re: Help getting custom cell layout in NSOutlineView column
  • Next by Date: [Job] Develop at Skype
  • Previous by thread: Re: How to deal with a MenuItem with both a binded state property and an action method
  • Next by thread: Re: How to deal with a MenuItem with both a binded state property and an action method
  • Index(es):
    • Date
    • Thread