문제

I'm trying to understand how NSArrayController works...

  • When I create an NSMutableArray "employeeArray" to hold "Person" object. enter image description here
  • I use NSArrayController in GUI and bind with the "employeeArray". enter image description here
  • I add two buttons for adding and removing "Person" objects in "employeeArray". Both buttons are binded with the received action "add:" and "remove:" of NSArrayController. enter image description here
  • Inorder to do something else when adding/removing "Person" objects in "employeeArray", I add two methods:
    • (void)insertObject:(Person *)p inEmployeeArrayAtIndex:(NSUInteger)index;
    • (void)removeObjectFromEmployeeArrayAtIndex:(NSUInteger)index;

Question:

  1. I didn't do anything(write any code) to let NSArrayController know the two methods above, why they are called automatically when I click the add/remove button? What happened?
  2. What add/remove method does NSArrayController use?
  3. Does NSArrayController smart enough to recognized those methods? What if I modified the method names randomly, will it be called correctly?
도움이 되었습니까?

해결책

I think I finally understand:

By following "Key-Value Coding Accessor Methods Patterns". NSArrayController is smart enough to recognize the "insert" "remove" methods automatically.

If you alter the method name without following the accessor pattern. It will not work.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top