我的iOS报亭应用程序中有一个选项供用户删除应用程序中的所有问题以释放空间。最初按下按钮会在没有警告的情况下删除所有内容,因此我决定添加一个警报视图,让他们有机会删除或取消。
我的警报工作正是我想要的,但选择"删除"没有触发该方法。谁能告诉我为什么?
这是我的代码:
- (void)showAlert
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Delete all issues?"
message:@"Your issues will be deleted to free up space on your device but can be re-downloaded as long as you are subscribed."
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Delete", nil];
[alert show];
}
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// Is this my Alert View?
if (alertView.tag == 100) {
//Yes
// You need to compare 'buttonIndex' & 0 to other value(1,2,3) if u have more buttons.
// Then u can check which button was pressed.
if (buttonIndex == 0) {
// 1st Other Button
} else if (buttonIndex == 1) {
// 2nd Other Button
[self performSelector:@selector(trashContent) withObject:nil afterDelay:0.01];
}
} else {
//No
// Other Alert View
}
}
- (void)trashContent
{
if (NewsstandApp) {
NKLibrary *nkLib = [NKLibrary sharedLibrary];
NSLog(@"%@",nkLib.issues);
[nkLib.issues enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[nkLib removeIssue:(NKIssue *)obj];
}];
[self.publisher addIssuesInNewsstand];
} else {
for (Issue *issue in self.publisher.issues) {
NSFileManager* fileManager = [NSFileManager defaultManager];
NSError *error;
[fileManager removeItemAtPath:issue.fileUrl error:&error];
if (!error) {
issue.downloadState = IssueStatusNone;
}
}
}
[self.collectionView reloadData];
}
答案 0 :(得分:2)
当您创建UIAlertView
时,您永远不会为其分配100
标记,因此当您在if (alertView.tag == 100)
中进行检查clickedButtonAtIndex:
时,它将始终返回FALSE
并且永远不要在你确定按下哪个按钮的第二个if语句中。因此,也要更改showAlert
:
- (void)showAlert
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Delete all issues?"
message:@"Your issues will be deleted to free up space on your device but can be re-downloaded as long as you are subscribed."
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Delete", nil];
[alert setTag:100]; // Add this line.
[alert show];
}
就个人而言,我实际上创建了一个常量并为其赋值常量,并检查是否有一个常量,如在实现文件的顶部声明const int deleteAllAlertTag = 100;
然后有[alert setTag:deleteAllAlertTag];
然后你可以{ {1}}。我这样做只是因为如果您决定更改alertView标记的值,则只需更改一次,您的代码仍然有效。
答案 1 :(得分:1)
你需要
alert.tag = 100;
在您创建它的代码中。如果这实际上是问题,那么你的问题不是为什么这个方法没有被触发,因为它是。使用调试器逐步执行代码会显示if
未能触发,因为标记未设置为100。