【发布时间】:2013-11-11 23:44:59
【问题描述】:
(iOS7,xCode 5.1)我有一个应用程序可以出于各种目的访问日历,并且我正在尝试将我的所有错误消息传递到位。
我有 2 个UIAlertviews。两个UIAlertviews 都会在我需要它们时显示,但我只接到didDismissWIthButtonIndex 的一个电话。名为_iCloudAlert 的警报视图是有效的。
如果我显示_iCloudAlert,我会在单击按钮时接到didDismissWIthButtonIndex 的电话,但是当我显示_deniedAccessAlert 时,我根本没有接到电话。我什至看不到最外面的NSLog/s。
我的.h 文件中有<UIAlertviewDelegate>。
显示警报的代码,取决于日历访问:
// Check the authorization status of our application for Calendar
-(void)checkEventStoreAccessForCalendar
{
NSLog(@"Check Status");
EKAuthorizationStatus status = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];
switch (status)
{
// Update our UI if the user has granted access to their Calendar
case EKAuthorizationStatusAuthorized: [self accessGrantedForCalendar];
NSLog(@"Already granted");
break;
// Prompt the user for access to Calendar if there is no definitive answer
case EKAuthorizationStatusNotDetermined: [self requestCalendarAccess];
break;
// Display a message if the user has denied or restricted access to Calendar
case EKAuthorizationStatusDenied:
case EKAuthorizationStatusRestricted:
{
NSLog(@"already denied");
[self performSelectorOnMainThread:@selector(showDeniedAccessAlert) withObject:nil waitUntilDone:NO];
}
break;
default:
break;
}
}
两种警报查看方法:
- (void)informUserAboutCloud {
_iCloudAlert = [[UIAlertView alloc]
initWithTitle: @"Important!"
message: @"If you have an iCloud account.....blah, blah, blah..."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[_iCloudAlert show];
}
- (void)showDeniedAccessAlert {
NSLog(@"Show Denied Access Alert");
_deniedAccessAlert = [[UIAlertView alloc]
initWithTitle: @"Attention!"
message: @"It looks like you've blocked access to Calendar data... Blah, Blah, Blah..."
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[_deniedAccessAlert show];
}
这里是用于对按钮点击采取行动的代码:
- (void)alertView:(UIAlertView *)alert didDismissWithButtonIndex:(NSInteger)buttonIndex {
NSLog(@"button index: %i", buttonIndex); //only logs when _iCloudAlert is shown
NSLog(@"alertview: %@", alert); //only logs when _iCloudAlert is shown
if (_iCloudAlert) {
[self checkEventStoreAccessForCalendar];
NSLog(@"check for calendar access from dismissed icloud alert...");
}
if (_deniedAccessAlert) {
NSLog(@"dismissed denied access..."); //never logged
}
}
【问题讨论】:
-
建议 - 不要为每个警报视图使用 ivars,不要使用 ivars。相反,为每个警报分配其自己的标记值。然后在委托方法中可以检查
alert参数的tag属性。 -
所以
if(alert.tag == 1),等等?与 ivars 相比,这样做的主要好处是什么? -
这真的是个人选择。我更喜欢简单地为我在一个类中可能拥有的每个警报标记定义一个新常量,而不是添加一个新的 ivar。如果需要,它还允许您在警报标签上使用
switch语句来处理每一个。当然,您可以使用 ivars 设置一组if-else语句。 -
很高兴知道。我会记住这一点的。
-
顺便说一句 - 不要忘记接受答案。
标签: ios uialertview