Does an IBOutlet needs to be a property & synthesized?

前端 未结 4 2580
隐瞒了意图╮
隐瞒了意图╮ 2020-11-29 16:37

In most examples I see the following setup of IBOutlets:



(Example A)

FooController.h:

@interface FooController : UIViewController {
    UILabel *fooLabel         


        
4条回答
  •  猫巷女王i
    2020-11-29 17:20

    On Mac OS X, IBOutlets are connected like this:

    1. Look for a method called set:. If it exists call it.
    2. If no method exists, look for an instance variable named , set it without retaining.

    On iPhone OS, IBOutlets are connected like this:

    1. call [object setValue:outletValue forKey:@""]

    The behavior of set value for key is to do something like this:

    1. Look for a method called set:. If it exists call it.
    2. If no method exists, look for an instance variable named , set it and retain it.

    If you use a property, you'll fall into the "Look for a method called set:..." case on both platforms. If you just use an instance variable, then you'll have different retain/release behavior on Mac OS X VS iPhone OS. There's nothing wrong with using an instance variable, you just need to deal with this difference in behavior as you switch between platforms.

    Here's a link to full documentation on just this topic. https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html#//apple_ref/doc/uid/10000051i-CH4-SW6

提交回复
热议问题