UIAlertView *view = [[[UIAlertView alloc] initWithTitle: @"Choose Configuration:" message: @""delegate: self cancelButtonTitle: @"Production" otherButtonTitles: @"Staging",
#if DEBUG
@"Development",
#endif
@"Reset Prefs and Quit", nil] autorelease];
[view setTag: 1];
#if BYPASS_CONFIG_ALERT
#warning Config alert bypassed!
[self alertView: view clickedButtonAtIndex: 2];
#else
[view show];
#endif
但是,当我们只需要提示信息,不需要响应一些事件时,可以将delegate:nil,设置成nil;
当我们需要对多个UIAlertView的多个button按钮响应事件时,我们此时就需要分清楚,当前显示的UIAlertView到底是哪一个的UIAlertView。此时,我们有一种方法,当我们定义UIAlertView时,我们可以设置它的tag。这样我们就可以使用tag来区分到底当前显示的是哪一个UIAlertView。
- (void) alertView:(UIAlertView *) inAlertView clickedButtonAtIndex:(NSInteger) buttonIndex
{
switch ( [inAlertView tag] )
{
case 0:
{
// network timeout alert
self.alert = nil;
switch ( buttonIndex )
{
case 0:
{
// cancel
exit( 0 );
} break;
case 1:
{
// retry
self.loadTime = [NSDate date];
self.timer = [NSTimer scheduledTimerWithTimeInterval:kXMLLookupTimeout target: self selector: @selector(timeout:) userInfo: nil repeats: NO];
} break;
}
} break;
case 1:
{
// debug preferences alert
NSUserDefaults *defaults;
NSString *key, *lastStartupMethod, *startupMethod;
NSEnumerator *keyEnumerator;
BOOL resetPreferences = NO;
defaults = [NSUserDefaults standardUserDefaults];
lastStartupMethod = [defaults objectForKey: @"lastStartupMethod"];
switch ( buttonIndex )
{
case 0:
{
// production config files
startupMethod = @"Release";
} break;
case 1:
{
// staging config files
startupMethod = @"Debug";
[ConfigFileFetcher setUseDebugFiles: YES directory:@"staging"];
} break;
#if DEBUG
case 2:
{
// development config files
startupMethod = @"Debug";
[ConfigFileFetcher setUseDebugFiles: YES directory:@"development"];
} break;
#endif
default:
{
// reset prefs & quit
startupMethod = nil;
resetPreferences = YES;
} break;
}
resetPreferences |= lastStartupMethod && ! [lastStartupMethodisEqualToString: startupMethod];
if ( resetPreferences )
{
keyEnumerator = [[defaults dictionaryRepresentation]keyEnumerator];
while ( ( key = [keyEnumerator nextObject] ) )
{
[defaults removeObjectForKey: key];
}
}
if ( startupMethod ) [defaults setObject: startupMethod forKey:@"lastStartupMethod"];
[defaults synchronize];
if ( buttonIndex == 3 )
{
exit( 0 );
} else
{
[self applicationDidFinishLaunchingActual: [UIApplicationsharedApplication]];
}
} break;
case kTagAlertTerminateApp:
{
// application too old alert
exit( 0 );
} break;
case kTagAlertVideo:
{
NSString *videoURL;
switch ( buttonIndex )
{
case 1:
videoURL =pendingVideoStation.videoURLLowBandwidth;break;// yes play low bandwidth video
case 2:
videoURL = pendingVideoStation.videoURL;break;// no play high bandwidth video
default:
videoURL = nil;break;// cancel
}
if ( videoURL )
{
[PlayerViewController shared].station = nil;
[VideoController showVideo: videoURL];
}
self.pendingVideoStation = nil;
} break;
}
}