用户点击按钮后,我想要一个动作表,要求他们在两个选项之间进行选择。一旦他们选择了一个选项,我想让AlertView告诉他们他们将离开应用程序并让他们选择取消操作或继续使用其他应用程序。
代码如下:
·H
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface mapView : UIViewController <MKMapViewDelegate, UIActionSheetDelegate, UIAlertViewDelegate>
@property (nonatomic, weak)IBOutlet MKMapView *mapView;
@property (nonatomic, weak)IBOutlet UIBarButtonItem *getDirections;
- (IBAction)selectDestination:(id)sender;
- (void)checkLeave;
@end
的.m
- (IBAction)selectDestination:(id)sender
{
UIActionSheet *selectDestinationAS = [[UIActionSheet alloc] initWithTitle:@"Select Destination: " delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Destination 1", @"Destination 2", nil];
[selectDestinationAS showInView:self.view];
}
- (void)checkLeave
{
UIAlertView *checkLeaveAlert = [[UIAlertView alloc] initWithTitle:@"Leave CDSI?" message:@"This will open the Maps application to continue directions. Are you sure you want to continue?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Open", nil];
[checkLeaveAlert show];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
[self checkLeave];
if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString: @"Destination 1"]) {
NSURL *BOHMDirections = [NSURL URLWithString:@"http://maps.apple.com/?daddr=Destination1&saddr=Current+Location"];
[[UIApplication sharedApplication] openURL:BOHMDirections];
} else if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString: @"Destination 2"]) {
NSURL *BOPDirections = [NSURL URLWithString:@"http://maps.apple.com/?daddr=Destination2&saddr=Current+Location"];
[[UIApplication sharedApplication] openURL:BOPDirections];
}
}
操作表显示,当选择一个选项时,地图应用程序会打开(根据需要),但只有在您重新输入原始应用程序后才会显示AlertView。在离开应用程序之前如何让它显示出来?
答案 0 :(得分:1)
导航到UIAlertView委托上的外部应用程序。
要在UIActionSheet传递所选项目索引,请在checkLeave方法中将索引作为参数传递,并将其设置为标记到UIAlertView
通过这种方式,UI执行将在ActionSheet单击时,警报视图向用户询问确认。一旦用户确认,将基于操作表选择执行导航。要保持,操作表选择,我们将该数据作为标记传递。
如果需要,可以添加一个私有属性来保存点击的项目数据并在UIAlertViewDelegate中访问它。
- (void)checkLeave :(NSInteger)index
{
UIAlertView *checkLeaveAlert = [[UIAlertView alloc] initWithTitle:@"Leave CDSI?" message:@"This will open the Maps application to continue directions. Are you sure you want to continue?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Open", nil];
[checkLeaveAlert setTag:index];
[checkLeaveAlert show];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
[self checkLeave : buttonIndex];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertView.tag == 1){
//Destination 1 clicked
}
}
答案 1 :(得分:1)
与操作表类似,UIAlertView具有您应该实施/遵守的委托协议。特别是方法alertView:didDismissWithButtonIndex:
是有意义的。
在那里调用地图应用,而不是clickedButtonAtIndex