Objective c 子类化UIAlertView并使用子类作为委托时出现问题
我有点小问题。我将其子类命名为UIAlertView,并将其命名为UIDeclaimeralertView 到目前为止,这个子类运行良好。但是现在我想使用这个类作为它自己的委托。所以在我的子类中,我使用了:Objective c 子类化UIAlertView并使用子类作为委托时出现问题,objective-c,cocoa-touch,delegates,uialertview,Objective C,Cocoa Touch,Delegates,Uialertview,我有点小问题。我将其子类命名为UIAlertView,并将其命名为UIDeclaimeralertView 到目前为止,这个子类运行良好。但是现在我想使用这个类作为它自己的委托。所以在我的子类中,我使用了:self.delegate=self我这样做是因为我希望它都在同一个类文件中 现在我面临的问题是我的委托的方法被调用,但是参数是(null) 例如: - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteg
self.delegate=self代码>我这样做是因为我希望它都在同一个类文件中
现在我面临的问题是我的委托的方法被调用,但是参数是(null)
例如:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"Trying: %@", buttonIndex);
}
每次返回(null)。你知道为什么吗?我还尝试了[super-setDelegate:self]代码>(直接告诉超类(UIAlertView)),它做同样的事情
以下是完整的实现文件:
#import "UIDisclaimerAlertView.h"
#import "PremierSoinsAppDelegate.h"
#import "BlocTexte.h"
@implementation UIDisclaimerAlertView
@synthesize dialogKey, plist, plistPath;
- (id)initWithKeyAndBlocTexteId:(NSString *)key BlocTexteId:(NSString *)blocTexteId
{
if (self = [super init])
{
// Fetching content
BlocTexte *blocTexte = [[BlocTexte alloc] init];
NSString *messageString = [[blocTexte getBlocTextes:blocTexteId] objectAtIndex:1];
// Setting superclass' properties
self.title = @"Disclaimer";
self.message = messageString;
[super setDelegate: self];
[self addButtonWithTitle: @"Ok"];
[self addButtonWithTitle: @"Toujours"];
// Setting plist key
self.dialogKey = key;
// Loading plist content
PremierSoinsAppDelegate *AppDelegate = (PremierSoinsAppDelegate *)[[UIApplication sharedApplication] delegate];
self.plistPath = [AppDelegate saveFilePath:@"disclaimers.plist"];
self.plist = [[NSMutableDictionary alloc] initWithContentsOfFile:self.plistPath];
[blocTexte release];
}
return self;
}
- (void)show {
[super show];
}
- (void)toujours
{
[self.plist setValue:@"1" forKey:self.dialogKey];
[self.plist writeToFile:self.plistPath atomically:YES];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"Trying: %@", buttonIndex);
}
@end
buttonIndex不是指针,因此%@
格式化程序不是正确的使用方法:您正在查找%d
或%i
。您可能会混淆NSInteger
——一种基本类型,在像iPhone这样的32位系统上,它的类型定义为int
,与NSNumber
,一种充当通用号码保持器的对象类型。哦,糟了,我太笨了。我总是犯同样的错误!谢谢你,它现在可以工作了。。。