2014-03-25 4 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

관련 문제