Ios 为UIAlertView设置委托
我正在更新新UIAlertController的代码,因为整个项目中有很多警报,所以我将警报代码拉到了它自己的类中。UIAlertController工作得很好,但是我在让旧的UIAlertView正常工作时遇到了问题 如果我使用self作为委托,代码就会崩溃。我怀疑是因为在调用委托时对象已经消失了。如果我使用self.navigationController,代码不会崩溃,但代理不会单击按钮。我在调用类中有相同的委托方法,它也没有被调用 我尝试将showAlert方法设置为类方法,但没有成功。我怀疑我可能需要将UIAlertView委托设置为调用类,但不知道如何执行Ios 为UIAlertView设置委托,ios,objective-c,delegates,Ios,Objective C,Delegates,我正在更新新UIAlertController的代码,因为整个项目中有很多警报,所以我将警报代码拉到了它自己的类中。UIAlertController工作得很好,但是我在让旧的UIAlertView正常工作时遇到了问题 如果我使用self作为委托,代码就会崩溃。我怀疑是因为在调用委托时对象已经消失了。如果我使用self.navigationController,代码不会崩溃,但代理不会单击按钮。我在调用类中有相同的委托方法,它也没有被调用 我尝试将showAlert方法设置为类方法,但没有成功。
@implementation ShowSystemAlert
- (instancetype)initWithNavigatioController:(UINavigationController *)navigationController
withManagedObjectContext:(NSManagedObjectContext *)context
withScoreKeeper:(ScoreKeeper *)scoreKeeper
withWordList:(WordList *)wordlist {
self = [super init];
if (self) {
_navigationController = navigationController;
_mObjContext = context;
_scoreKeeper = scoreKeeper;
_wordList = wordlist;
}
return self;
}
- (void)showAlert {
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
// code for UIAlertController goes here
[self.navigationController presentViewController:alert animated:true completion:nil];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"'Only Show Favorites'\nTurned Off"
message:@"aWe looked for words in the categories you selected but did not find any. We turned off 'Only Show Favorites' and found some words."
self.navigationController
cancelButtonTitle:@"Settings"
otherButtonTitles:@"Continue", nil];
[alert show];
}
}
#pragma mark - No Words
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"In alertView delegate");
if (buttonIndex == 0) {
NSLog(@"Cancel buttonpressed on no words");
PrefsTableViewController *ptvc = [[PrefsTableViewController alloc]
initWithWordList:self.wordList];
[self.navigationController pushViewController:ptvc animated:YES];
}
}
@end
在iOS8下,请将您的委托方法ClickedButtonIndex更改为didDismissWithButtonIndex 把这行改成
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
用这个
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
你应该会发现这将解决崩溃
我希望这能有所帮助。我认为您正确地诊断了问题。问题很可能是实例在调用委托方法之前被释放。创建ShowSystemAlert实例时,请将其分配给viewController的一个强变量,这样它就不会被释放。您发布的代码将产生语法错误,因为方法调用是非法的。感谢Mathias,这样做了。其中一个例子是,既然我知道了答案,我想知道为什么我花了一整天的时间来研究这个问题,而解决方案是如此明显。这是不对的,是吗?我很确定UIAlertViewDelegate需要在@interface ShowSystemAlert这样的界面上运行。对不起,大力水手。漫长的一天,是的,这需要在@interface中。我之前试过了,但没有成功。马蒂亚斯的建议成功了。很高兴你修好了。以前,dismissWithButton为我解决了一个类似的问题,我认为这可能是相同的。干杯
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {