开发者

Need declare protocol in objective c?

I wonder if it is needed to declare the protocol.

So, I delete a part that protocol declare code.

I thought that it will happen compile error.

but, Has been running without any problems.

Why need protocol declare part?

#import <UIKit/UIKit.h>


@interface SingleComponentPickerViewController : UIViewController {
// <UIPickerViewDelegate, UIPickerViewDataSource> {   ==> protocol declare part
    IBOutlet    UIPickerView *singlePicker;
                NSArray *picke开发者_如何学CrData;
}
@property (nonatomic, retain) UIPickerView *singlePicker;
@property (nonatomic, retain) NSArray *pickerData;
- (IBAction)buttonPressed:(id)sender;
@end


<UIPickerViewDelegate, UIPickerViewDataSource> <= this is what feature?
                                                  When omit this, excute without any problem. 

Below is picture After compiled screen.

It has no issues.

And excute without any problems..

Need declare protocol in objective c?


You are not declaring the Protocol here, you are merely specifying that the your class SingleComponentPickerViewController follows the protocols you specify.

The protocol needs to be specified if you are using any components and are setting you SingleComponentPickerViewController as their delegate. This is so the compiler knows that your class follows that specific protocol and you have implemented any required protocol methods.


<UIPickerViewDelegate, UIPickerViewDataSource>

It doesn't declare a protocol, it tells the compiler that your class conforms to the listed protocols.

It's needed, so other classes can know your class conforms to their protocol(s). If you omit them, you won't get a compiler error, but the delegate methods you declared in your class may not be called.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜