我以爲終於理解了委託的概念,直到發生以下情況:我更改了頭文件以刪除對委託的引用,並且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