我开始学习目标-C uikit,我遇到了一个problem.UIAlertViewDelegate而不是called.Can,有人告诉我为什么?谢谢!
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface AlertLearn : NSObject <UIAlertViewDelegate>
-(void) showAlertTest;
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
@end
#import "AlertLearn.h"
#import <UIKit/UIKit.h>
@implementation AlertLearn
-(void) showAlertTest{
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"alert" message:@"alert test" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
[alertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(@"clickedButtonAtIndex = %ld",buttonIndex );
NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
NSLog(@"clickedButtonAtIndex title = %@",buttonTitle);
}
@end发布于 2015-10-10 08:51:10
这是因为您要在比预期更严格的生命周期内分配警报视图,请尝试将警报设置为iVar或属性,然后再试一次。
@interface AlertLearn : NSObject <UIAlertViewDelegate>
{
UIAlertView * alertView;
}
-(void) showAlertTest;
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
@end
...
-(void) showAlertTest{
alertView = [[UIAlertView alloc] initWithTitle:@"alert" message:@"alert test" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
[alertView show];
}并从showAlertTest方法中删除定义。
P.S.:请注意,这种类型的显示警告视图在iOS 8中被取消,您应该使用UIAlertController,但是如果出于任何原因您需要支持iOS 7及以下,那么您应该为您的警告做一个强指针。
https://stackoverflow.com/questions/33052066
复制相似问题