Objective-C/Cocoa: Proper design for delegates and controllers

前端 未结 4 2027
余生分开走
余生分开走 2020-12-28 23:11

Consider the following common situation:

You have some MainView in your Cocoa application, loaded from a NIB, which is controlled by a MainViewCon

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-28 23:51

    In an unnamed developer forum, someone writes:

    So, to make a long story short, I have decided that I will start making use of NSNotifications. The Stanford course online that people have been following is taught by two Apple engineers. They have just now unequivocally said NOT to use the app delegate or global variables, and have said to use NSNotifications, delegates, and K-V observing.

    If that is what the Apple engineers say, I am going to move in that direction.

    The NSNotifications are pretty ingenious in that they don't really interfere with encapsulation that much. The listener only listens for the notification and an object - I don't think it has to know or care who sent it.

    So in your example I would consider having the delegate post a notification that the label had changed, or better yet have the controller observe that property if possible.

提交回复
热议问题