2014-03-25 92 views
0

用戶單擊按鈕後,我想要一個操作表出現,要求他們在兩個選項之間進行選擇。一旦他們選擇了一個選項,我想要一個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。如何在離開應用程序之前讓它顯示出來?

+0

哪裏UIAlertView中的委託方法?您沒有處理來自警報視圖的響應 - 如果視圖在應用程序退出前顯示 - 按取消將不會執行任何操作。 – Tander

回答

1

導航到UIAlertView代理的外部應用程序。

要傳遞所選擇的項目的索引處UIActionSheet,通過指數如checkLeave方法參數並設置爲標籤向UIAlertView中

通過這種方式,UI執行將上ActionSheet點擊時,alertview詢問確認與用戶。一旦用戶確認,導航將根據操作表選擇進行。爲了保留動作片選擇,我們將該數據作爲標籤傳遞。

如果需要,您可以添加一個私有屬性來保存點擊的項目數據並在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 
    } 
} 
+0

謝謝!除了'alertView.tag == 1'之外,所有這一切都實際上等同於我的第二個目的地。 – slichlyter12

1

與操作表類似,UIAlertView具有您應該執行/遵守的委託協議。特別感興趣的是方法alertView:didDismissWithButtonIndex:

調用地圖應用出現,而不是在clickedButtonAtIndex