From 589fdff1b3cccf6f510ad7a864b7100d53100d2d Mon Sep 17 00:00:00 2001 From: Scott Ludwig Date: Wed, 20 Jan 2016 11:00:11 -0800 Subject: [PATCH] UIAlert -> UIAlertController deprecation fix --- game/iphone/inputcontroller.h | 5 ++- game/iphone/inputcontroller.mm | 60 +++++++++++++++++++++------------ game/iphone/wiviewcontroller.mm | 2 +- 3 files changed, 41 insertions(+), 26 deletions(-) diff --git a/game/iphone/inputcontroller.h b/game/iphone/inputcontroller.h index 0fe5da8..401cbc4 100644 --- a/game/iphone/inputcontroller.h +++ b/game/iphone/inputcontroller.h @@ -19,14 +19,13 @@ NSString *default_string_; UIKeyboardType keyboard_type_; BOOL secure_; - UIAlertView *alert_view_; int cchMax_; id delegate_; } - (id)init:(NSString *)title default:(NSString *)default_string keyboardType:(UIKeyboardType)keyboard_type delegate:(id)delegate - maxChars:(int)cchMax secure:(BOOL)secure; -- (void)loadView; + maxChars:(int)cchMax secure:(BOOL)secure controller:(UIViewController *)controller; +- (void)loadAlert:(UIViewController *)controller; @end #endif // __INPUTCONTROLLER_H__ diff --git a/game/iphone/inputcontroller.mm b/game/iphone/inputcontroller.mm index 3e8d45b..332155d 100644 --- a/game/iphone/inputcontroller.mm +++ b/game/iphone/inputcontroller.mm @@ -4,7 +4,7 @@ - (id)init:(NSString *)title default:(NSString *)default_string keyboardType:(UIKeyboardType)keyboard_type delegate:(id)delegate - maxChars:(int)cchMax secure:(BOOL)secure { + maxChars:(int)cchMax secure:(BOOL)secure controller:(UIViewController *)controller { title_ = title; [title_ retain]; @@ -15,9 +15,8 @@ [delegate_ retain]; cchMax_ = cchMax; secure_ = secure; - alert_view_ = nil; - [self loadView]; + [self loadAlert:controller]; return self; } @@ -25,31 +24,48 @@ - (void)dealloc { [title_ release]; [default_string_ release]; - [alert_view_ release]; [delegate_ release]; [super dealloc]; } -- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)way -{ - return way == UIInterfaceOrientationLandscapeRight; -} +- (void)loadAlert:(UIViewController *)controller { + UIAlertController *alert = [UIAlertController + alertControllerWithTitle:title_ + message:nil + preferredStyle:UIAlertControllerStyleAlert]; -- (void)loadView { - UIAlertView *view = [[UIAlertView alloc] initWithTitle:title_ - message:nil delegate:self cancelButtonTitle:@"Cancel" - otherButtonTitles:@"OK", nil]; - alert_view_ = view; - alert_view_.alertViewStyle = secure_ ? UIAlertViewStyleSecureTextInput : UIAlertViewStylePlainTextInput; - [alert_view_ textFieldAtIndex:0].delegate = self; - [alert_view_ show]; -} + [alert addTextFieldWithConfigurationHandler:^(UITextField *text_field) { + text_field.text = default_string_; + text_field.keyboardType = keyboard_type_; + text_field.autocapitalizationType = UITextAutocapitalizationTypeNone; + text_field.autocorrectionType = UITextAutocorrectionTypeNo; + text_field.spellCheckingType = UITextSpellCheckingTypeNo; + text_field.enablesReturnKeyAutomatically = YES; + if (secure_) { + [text_field setSecureTextEntry:YES]; + } + text_field.delegate = self; + }]; -- (void)alertView:(UIAlertView *)actionSheet - clickedButtonAtIndex:(NSInteger)buttonIndex { - if (buttonIndex > 0) { - [delegate_ onDone:self text:[alert_view_ textFieldAtIndex:0].text]; - } + UIAlertAction *cancel_action = [UIAlertAction + actionWithTitle:@"Cancel" + style:UIAlertActionStyleDefault + handler:^(UIAlertAction *action) { + }]; + [alert addAction:cancel_action]; + + UIAlertAction *ok_action = [UIAlertAction + actionWithTitle:@"OK" + style:UIAlertActionStyleDefault + handler:^(UIAlertAction *action) { + UITextField *text_field = [alert.textFields firstObject]; + [delegate_ onDone:self text:text_field.text]; + }]; + [alert addAction:ok_action]; + + [controller presentViewController:alert animated:YES completion:nil]; + UITextField *text_field = [alert.textFields firstObject]; + [text_field becomeFirstResponder]; } - (BOOL)textField:(UITextField *)textField diff --git a/game/iphone/wiviewcontroller.mm b/game/iphone/wiviewcontroller.mm index e0285e7..8c9ecaa 100644 --- a/game/iphone/wiviewcontroller.mm +++ b/game/iphone/wiviewcontroller.mm @@ -128,7 +128,7 @@ InputController *input_controller = [[InputController alloc] init:title default:def keyboardType:keyboardType - delegate:self maxChars:maxAsk_ secure:secureAsk_]; + delegate:self maxChars:maxAsk_ secure:secureAsk_ controller:self]; input_controller_ = input_controller; }