我UIAlertView
在课堂上的委派方面遇到了困难ViewController
。一切都很好,直到用户单击OK
按钮 - 然后应用程序崩溃
Thread 1: EXC_BAD_ACCESS (code=2, address 0x8)
视图控制器.h:
#import <UIKit/UIKit.h>
#import "DataModel.h"
@interface ViewController : UIViewController
@end
视图控制器.m:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
DataModel *dataModel = [[DataModel alloc] init];
[dataModel ShowMeAlert];
[super viewDidLoad];
}
@end
数据模型.h
#import <Foundation/Foundation.h>
@interface DataModel : NSObject <UIAlertViewDelegate>
- (void)ShowMeAlert;
@end
数据模型.m
#import "DataModel.h"
@implementation DataModel
- (void)ShowMeAlert;
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Info" message:@"View did load!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
#pragma mark - UIAlertView protocol
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSLog(@"Index: %d", buttonIndex);
}
@end
- 如果显示警报的代码及其委托方法在
ViewController
- 完美运行。 - 当我删除
UIAlertDelegation
方法时...didDismissWithButtonIndex...
- 无需授权即可工作。 - 当我设置
UIAlertView delegate
为nil
- 无需授权即可工作。
有什么线索有什么问题吗?