Why don't I need to declare a UIAlertViewDelegate in the header?

I thought I finally managed to understand the delegate concept until the following happened: I changed my header file to remove the delegate link, and Alert was still working. The only difference is that I am losing the code hint.

//.h #import <UIKit/UIKit.h> //@interface ViewController : UIViewController <UIAlertViewDelegate> @interface ViewController : UIViewController - (IBAction)showMessage:(id)sender; @end //.m #import "ViewController.h" @implementation ViewController - (IBAction)showMessage:(id)sender { UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Hello World!" message:@"Message." delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Button 1", @"Button 2", nil]; [message show]; } - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { NSString *title = [alertView buttonTitleAtIndex:buttonIndex]; if([title isEqualToString:@"Button 1"]) { NSLog(@"Button 1 was selected."); } } @end 
+4
source share
4 answers

<UIAlertViewDelegate> in your header is just an indication to the compiler that you intend to implement the delegate methods in your class. You will receive warnings if you do not implement the delegation methods marked as @required, but since most delegate methods are usually @optional, your code will compile and work fine. This does not mean that you should not add delegates to your headline, though.

+9
source

While you have already accepted the answer, there is more to this problem than there.

UIAlertViewDelegate is a protocol that implements the delegate design template. You may need to officially inform the runtime environment that you comply with any given protocol (especially if it does not have the necessary methods), but it depends on the design of the class that the protocol declares. You accept the protocol in your class by putting the protocol name in <> when declaring the class, for example:

 @interface MyClass : NSObject <delegateProtocolName> 

Because many delegated protocol methods are optional, they often check to see if the accepted class implements a particular method as follows:

 if ([self.delegate respondsToSelector:@selector(delegatedMethod:)]) { // Do something } 

In this case, you do not need to match the protocol in your header file because it checks to see if a specific delegation method has been implemented.

However, the test can be written in the same way (especially if you need to refer to several required methods / properties in the same function):

 if ([self.delegate conformsToProtocol:@protocol(delegateProtocolName)]) { // Do something } 

In this case, you must comply with the protocol in your header file or not pass the test.

To quote the documentation for conformsToProtocol (taken from Objective-C programming language and emphasis added by me):

This method only matches based on a formal declaration in the header files , as shown above. He does not check to see if the methods declared in the protocol are really the responsibility of the programmers.

+6
source

Answer: the apple does not want to require the implementation class of the UIAlertViewDelegate protocol. If Apple wishes to request , this will make the UIAlertView delegation property of type id<UIAlertViewDelegate> . If you look at the documentation, it is not.

UIAlertView class reference

@property(nonatomic, assign) id delegate

They must have reasons for not doing this @property(nonatomic, assign) id<UIAlertViewDelegate> delegate .

+3
source

The <UIAlertViewDelegate> that you mentioned in your class means that you implement the AlertView delegation methods in this class, i.e. ViewController and delegate: self means that the delegate methods of this object are defined in the current class.

If you want to define the AlertView delegation method in any other class, you must specify <UIAlertViewDelegate> in this class and implement the methods in this particular class.

and you must also change the delegate: (file_name).

+1
source

Source: https://habr.com/ru/post/1413022/


All Articles