gpt4 book ai didi

objective-c - 为什么我不需要在 header 中声明 UIAlertViewDelegate?

转载 作者:可可西里 更新时间:2023-11-01 05:01:24 25 4
gpt4 key购买 nike

在发生以下情况之前,我以为我终于设法理解了委托(delegate)的概念:我更改了头文件以删除对委托(delegate)的引用,并且 Alert 仍然有效。唯一的区别是我丢失了代码提示。

//.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

最佳答案

<UIAlertViewDelegate>在你的标题中只是向编译器表明你打算在你的类中实现委托(delegate)方法。如果您不实现标记为@required 的委托(delegate)方法,您将收到警告,但由于大多数委托(delegate)方法通常是@optional,您的代码将编译并运行良好。不过,这并不意味着您不应该在 header 中添加委托(delegate)。

关于objective-c - 为什么我不需要在 header 中声明 UIAlertViewDelegate?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10639886/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com