我以为终于理解了委托的概念,直到发生以下情况:我更改了头文件以删除对委托的引用,并且Alert仍然有效。唯一的区别是我失去了代码提示。为什么我不需要在头文件中声明UIAlertViewDelegate?
//.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
是的...删除它会在构建时生成警告。如果你失去语法突出显示,那是因为Xcode。这不是一种语言功能。 – CodaFi