用戶單擊按鈕後,我想要一個操作表出現,要求他們在兩個選項之間進行選擇。一旦他們選擇了一個選項,我想要一個AlertView來告訴他們他們將離開應用程序,讓他們選擇取消操作或繼續到另一個應用程序。按下UIActionSheet按鈕後顯示UIAlertView
代碼如下:
.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];
}
}
的ActionSheet顯示時,選擇了選項時,當地圖應用打開(如需要的話),但僅當您重新輸入原始應用程序後纔會顯示AlertView。如何在離開應用程序之前讓它顯示出來?
哪裏UIAlertView中的委託方法?您沒有處理來自警報視圖的響應 - 如果視圖在應用程序退出前顯示 - 按取消將不會執行任何操作。 – Tander