From 1c670aa479608cf89b375e6872a177e537c5296b Mon Sep 17 00:00:00 2001 From: Alex Zolotarev Date: Tue, 22 Mar 2011 15:50:51 +0000 Subject: [PATCH] [iPhone] Added AlertView which cancels automatically when app goes to background --- iphone/Common/CustomAlertView.h | 13 +++++++++ iphone/Common/CustomAlertView.mm | 34 ++++++++++++++++++++++ iphone/Common/README.txt | 1 + iphone/Maps/Maps.xcodeproj/project.pbxproj | 15 ++++++++++ 4 files changed, 63 insertions(+) create mode 100644 iphone/Common/CustomAlertView.h create mode 100644 iphone/Common/CustomAlertView.mm create mode 100644 iphone/Common/README.txt diff --git a/iphone/Common/CustomAlertView.h b/iphone/Common/CustomAlertView.h new file mode 100644 index 0000000000..a167431f0c --- /dev/null +++ b/iphone/Common/CustomAlertView.h @@ -0,0 +1,13 @@ +#import + +/// Alert View which can automatically close when going to background +/// and call CancelButtonIndex delegate +@interface CustomAlertView : UIAlertView +{ +} + +- (id) initWithTitle:(NSString *)title message:(NSString *)message + delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle + otherButtonTitles:(NSString *)otherButtonTitles, ...; + +@end diff --git a/iphone/Common/CustomAlertView.mm b/iphone/Common/CustomAlertView.mm new file mode 100644 index 0000000000..16f050a533 --- /dev/null +++ b/iphone/Common/CustomAlertView.mm @@ -0,0 +1,34 @@ +#import "CustomAlertView.h" + +@implementation CustomAlertView + +- (id) initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate + cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... +{ + if ((self = [super initWithTitle:title message:message delegate:delegate + cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil, nil])) + { + va_list args; + va_start(args, otherButtonTitles); + for (NSString * anOtherButtonTitle = otherButtonTitles; anOtherButtonTitle != nil; anOtherButtonTitle = va_arg(args, NSString*)) + [self addButtonWithTitle:anOtherButtonTitle]; + + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground:) + name:UIApplicationDidEnterBackgroundNotification object:nil]; + } + return self; +} + +- (void) dealloc +{ + [[NSNotificationCenter defaultCenter] removeObserver:self]; + [super dealloc]; +} + +- (void) applicationDidEnterBackground:(id) sender +{ + // We should not be here when entering back to foreground state + [self dismissWithClickedButtonIndex:[self cancelButtonIndex] animated:NO]; +} + +@end \ No newline at end of file diff --git a/iphone/Common/README.txt b/iphone/Common/README.txt new file mode 100644 index 0000000000..62817991fe --- /dev/null +++ b/iphone/Common/README.txt @@ -0,0 +1 @@ +Contains reusable code snippets for all iOS projects \ No newline at end of file diff --git a/iphone/Maps/Maps.xcodeproj/project.pbxproj b/iphone/Maps/Maps.xcodeproj/project.pbxproj index 7931a3aaf6..fb0e679a20 100644 --- a/iphone/Maps/Maps.xcodeproj/project.pbxproj +++ b/iphone/Maps/Maps.xcodeproj/project.pbxproj @@ -68,6 +68,7 @@ FA0660001286167A00FEA989 /* Default.png in Resources */ = {isa = PBXBuildFile; fileRef = FA065FFE1286167A00FEA989 /* Default.png */; }; FA0660031286168700FEA989 /* Default-Portrait.png in Resources */ = {isa = PBXBuildFile; fileRef = FA0660011286168700FEA989 /* Default-Portrait.png */; }; FA0660041286168700FEA989 /* Default-Landscape.png in Resources */ = {isa = PBXBuildFile; fileRef = FA0660021286168700FEA989 /* Default-Landscape.png */; }; + FA34BECA1338D72F00FFB2A7 /* CustomAlertView.mm in Sources */ = {isa = PBXBuildFile; fileRef = FA34BEC81338D72F00FFB2A7 /* CustomAlertView.mm */; }; FA4135EA120A263C0062D5B4 /* CountriesViewController.mm in Sources */ = {isa = PBXBuildFile; fileRef = FA4135E2120A263C0062D5B4 /* CountriesViewController.mm */; }; FA4135ED120A263C0062D5B4 /* SettingsManager.mm in Sources */ = {isa = PBXBuildFile; fileRef = FA4135E7120A263C0062D5B4 /* SettingsManager.mm */; }; FA46DA0512D414D200968C36 /* maps.update in Resources */ = {isa = PBXBuildFile; fileRef = FA46DA0412D414D200968C36 /* maps.update */; }; @@ -163,6 +164,8 @@ FA065FFE1286167A00FEA989 /* Default.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Default.png; sourceTree = SOURCE_ROOT; }; FA0660011286168700FEA989 /* Default-Portrait.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default-Portrait.png"; sourceTree = SOURCE_ROOT; }; FA0660021286168700FEA989 /* Default-Landscape.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default-Landscape.png"; sourceTree = SOURCE_ROOT; }; + FA34BEC81338D72F00FFB2A7 /* CustomAlertView.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = CustomAlertView.mm; sourceTree = ""; }; + FA34BEC91338D72F00FFB2A7 /* CustomAlertView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CustomAlertView.h; sourceTree = ""; }; FA4135E1120A263C0062D5B4 /* CountriesViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; name = CountriesViewController.h; path = Settings/CountriesViewController.h; sourceTree = SOURCE_ROOT; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; }; FA4135E2120A263C0062D5B4 /* CountriesViewController.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; lineEnding = 0; name = CountriesViewController.mm; path = Settings/CountriesViewController.mm; sourceTree = SOURCE_ROOT; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; }; FA4135E6120A263C0062D5B4 /* SettingsManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SettingsManager.h; path = Settings/SettingsManager.h; sourceTree = SOURCE_ROOT; }; @@ -252,6 +255,7 @@ 29B97314FDCFA39411CA2CEA /* Maps */ = { isa = PBXGroup; children = ( + FA34BEC71338D6DB00FFB2A7 /* Common */, FA6E1F1B124E6B2800F59149 /* Platform */, FA4135DF120A25B90062D5B4 /* Settings */, 080E96DDFE201D6D7F000001 /* Classes */, @@ -377,6 +381,16 @@ name = "Static Libraries"; sourceTree = ""; }; + FA34BEC71338D6DB00FFB2A7 /* Common */ = { + isa = PBXGroup; + children = ( + FA34BEC81338D72F00FFB2A7 /* CustomAlertView.mm */, + FA34BEC91338D72F00FFB2A7 /* CustomAlertView.h */, + ); + name = Common; + path = ../Common; + sourceTree = SOURCE_ROOT; + }; FA4135DF120A25B90062D5B4 /* Settings */ = { isa = PBXGroup; children = ( @@ -526,6 +540,7 @@ FAF37F01126DCE6F005EA154 /* IPhonePlatform.mm in Sources */, 49DD2B51132FA8880031D82E /* GuideViewController.mm in Sources */, FAFCB63613366E78001A5C59 /* WebViewController.mm in Sources */, + FA34BECA1338D72F00FFB2A7 /* CustomAlertView.mm in Sources */, ); runOnlyForDeploymentPostprocessing = 0; };