除了UIAlertView之外,我在课堂上遇到了ViewController委托的困难。一切都很好,直到用户单击OK按钮-然后应用程序崩溃
Thread 1: EXC_BAD_ACCESS (code=2, address 0x8)ViewController.h:
#import <UIKit/UIKit.h>
#import "DataModel.h"
@interface ViewController : UIViewController
@endViewController.m:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
DataModel *dataModel = [[DataModel alloc] init];
[dataModel ShowMeAlert];
[super viewDidLoad];
}
@endDataModel.h
#import <Foundation/Foundation.h>
@interface DataModel : NSObject <UIAlertViewDelegate>
- (void)ShowMeAlert;
@endDataModel.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);
}
@endViewController中的,那么工作得很好。UIAlertDelegation方法时,...didDismissWithButtonIndex... -在没有委托的情况下工作。UIAlertView delegate设置为nil时--在没有委托的情况下工作。有什么线索吗?
发布于 2012-10-10 10:23:57
在这种方法中:
- (void)viewDidLoad
{
DataModel *dataModel = [[DataModel alloc] init];
[dataModel ShowMeAlert];
[super viewDidLoad];
}您正在分配一个DataModel局部变量,该局部变量将在作用域结束时由ARC解除分配。因此,当执行解散时,您的委托就不再存在了。修复方法是将DataModel存储在视图控制器的strong属性中。这样,它就不会被取消。你要做的是:
- (void)viewDidLoad
{
self.dataModel = [[DataModel alloc] init];
[self.dataModel ShowMeAlert];
[super viewDidLoad];
}https://stackoverflow.com/questions/12817033
复制相似问题