2017-10-18 8 views
2

最近、私は最終的に1つのレガシープロジェクトをリファクタリングし、すべてUIAlertViewsUIActionSheetsUIAlertControllersに変更しました。しかし、私は顧客からの問題を受け取るようになった、彼らは時々UIAlertControllerアクションタイトルが空白であると言っている。その方法のように:UIAlertControllerはUIAlertActionsの空白のタイトルを表示します

blank actions titles example

私は、あなたが可能な理由についてどんな考えを持っていない、私のデバイス上でこのバグを再現することはできませんか?

UPDATE:

私は信じて、その問題は多分、メインウィンドウのtintColorは何とかデフォルトではない、とUIAlertControllerはそれを継承し、そのコードに覆われていますか?

@implementation UIAlertController (WMC) 

    - (void)show:(BOOL)animated { 
     self.alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen 
    mainScreen].bounds]; 
     self.alertWindow.rootViewController = [[UIViewController alloc] init]; 

     id<UIApplicationDelegate> delegate = [UIApplication sharedApplication].delegate; 
     // Applications that does not load with UIMainStoryboardFile might not have a window property: 
     if ([delegate respondsToSelector:@selector(window)]) { 
      // we inherit the main window's tintColor 
      self.alertWindow.tintColor = delegate.window.tintColor; 
     } 

     // window level is above the top window (this makes the alert, if it's a sheet, show over the keyboard) 
     UIWindow *topWindow = [UIApplication sharedApplication].windows.lastObject; 
     self.alertWindow.windowLevel = topWindow.windowLevel + 1; 

     [self.alertWindow makeKeyAndVisible]; 
     [self.alertWindow.rootViewController presentViewController:self animated:animated completion:nil]; 
    } 

    @end 
+0

いくつかのコードを試してみることができますか? –

+0

例:http://hayageek.com/uialertcontroller-example-ios/ –

答えて

1

最後に、私は解決策を見つけたこのコードを試してみてください。いくつかの実験の後で、私は、UIWindowtintColorプロパティを設定すると、のすべてのUIAlertActionsのtintColorが変更されることに気付きました。だから、私はUIAlertControllerのコードを変更しました(注:元のコードは私のものではありませんでした。SoFの著者に感謝しました)。

#import "UIAlertController+WMC.h" 

@implementation UIAlertController (Private) 

@dynamic alertWindow; 

- (void)setAlertWindow:(UIWindow *)alertWindow { 
    objc_setAssociatedObject(self, @selector(alertWindow), alertWindow, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 
} 

- (UIWindow *)alertWindow { 
    return objc_getAssociatedObject(self, @selector(alertWindow)); 
} 

@end 

@implementation UIAlertController (WMC) 

- (void)show { 
    [self show:YES]; 
} 

- (void)show:(BOOL)animated { 
    self.alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; 
    self.alertWindow.rootViewController = [[UIViewController alloc] init]; 

    // window level is above the top window (this makes the alert, if it's a sheet, show over the keyboard) 
    UIWindow *topWindow = [UIApplication sharedApplication].windows.lastObject; 
    self.alertWindow.windowLevel = topWindow.windowLevel + 1; 

    [self.alertWindow makeKeyAndVisible]; 
    [self.alertWindow.rootViewController presentViewController:self animated:animated completion:nil]; 
} 

- (void)viewDidDisappear:(BOOL)animated { 
    [super viewDidDisappear:animated]; 

    // precaution to insure window gets destroyed 
    self.alertWindow.hidden = YES; 
    self.alertWindow = nil; 
} 

私が正確にしたことは、メインのウィンドウtintColorを継承する部分が削除されたことです。アラートを表示するそのような複雑な方法は、私のプロジェクトでナビゲーションがひどく構造化された結果です。質問に興味のあるすべての解説者に感謝します。

1

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Title Message" preferredStyle:UIAlertControllerStyleActionSheet]; 

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel action") style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){ 
     NSLog(@"Cancle Tapped"); 
     [alertController dismissViewControllerAnimated:YES completion:nil]; 
    }]; 

    [alertController addAction:cancelAction]; 

    UIAlertAction *yesAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Yes", @"Yes action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){ 
     NSLog(@"Yes Tapped"); 
    }]; 
    [alertController addAction:yesAction]; 

    UIAlertAction *noAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"No", @"No action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){ 
     NSLog(@"No Tapped"); 
    }]; 
    [alertController addAction:noAction]; 
    [self presentViewController:alertController animated:YES completion:nil]; 
+0

私のコードはまったく同じ方法で書かれていますが、私はUIAlertControllerにとても精通していますが、この問題は奇妙であり、まだ分かりません –

+0

@AleksandrSmirnov、あなたの顧客が空白のタイトルを取得するデバイスを教えてください。 – Kuldeep

+0

実質的にすべてのモデルで@kuldeep –

関連する問題