2014-10-07 103 views
11

我有一個UIAlertView在iOS 7中完美顯示,但在iOS 8中,它不顯示任何按鈕或標籤。警報仍然可見,但只是一個小白盒子。 「確定」和「取消」按鈕也將其事件作爲參考,但沒有文字可見。iOS 8:UIAlertView/UIAlertController不顯示文本或按鈕

我已經使用這個警報就按一下按鈕顯示

- (IBAction)sel_btnLogout:(id)sender { 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Logout!" message:@"Are you sure you want to logout?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil]; 
    [alert show]; 
} 

我查了框架中的iOS 8:這是給(0,0,0,0),但在iOS的7這是給一個確定的價值。

我也檢查迭代到uialertview的子視圖。在iOS7中,它會進入循環,因爲它會發現警報的子視圖。在iOS8中,它表示沒有alertView的子視圖。

+0

這是真正的密碼?如果不是,請發佈實際代碼。 – rmaddy 2014-10-07 04:52:40

+0

@siddhant發佈真實的代碼。此代碼也在iOS 8中工作。因此,請在此複製您在項目中編寫的確切代碼。 – Jasmeet 2014-10-07 04:53:36

+0

試試這個http://stackoverflow.com/questions/25927676/uialertview-title-not-display/25928050#25928050 – 2014-10-07 04:59:29

回答

4

我得到了我的問題的答案。問題是我在我的項目中使用了UIFont + Replacement類。這在iOS 7上工作正常,但在iOS 8上它使用了幾個不推薦使用的方法。由於這個原因,我不知道爲什麼,但只有我的警報視圖沒有顯示任何標籤。

解決方法:從項目中刪除類別,並通過xib設置字體。一旦我們將任何字體的.tff文件放入我們的項目工作區中,我們就會在自定義字體下的xib中看到這些字體名稱。無需使用UIFont +替換類別。

+0

它的工作原理:D。 Siddhant非常感謝你。我瘋了! – 2014-10-21 15:51:08

+1

@AlexGuerra - 不客氣。有點奇怪的問題,甚至更奇怪的解決方案:) – Siddhant 2014-10-24 08:50:26

-5

我認爲這不是UIAlertview問題。 請把這個代碼viewDidLoad像下面任何view controller檢查這項工作很好..

我覺得你的代碼問題...........

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"My message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil]; 
    [alert show]; 
} 
+0

1)不要顯示來自'viewDidLoad'的警報。該視圖尚未顯示。 2)這是如何回答這個問題的? OP聲明代碼在iOS 7下工作,所以它不會顯示警報的位置。 – rmaddy 2014-10-07 05:02:21

+0

@rmaddy - Alert仍然顯示在iOS8中,但沒有顯示任何文本或按鈕標籤。我檢查了框架......它給(0,0,0,0) – Siddhant 2014-10-07 05:26:20

+0

@Siddhant把你的問題的信息,而不是這個無關的答案。 – rmaddy 2014-10-07 05:29:00

2

在iOS 8中,您需要用UIAlertcontroller替換UIAletrviewUIActionSheet。您先閱讀在蘋果論壇本文檔
Apple Alertcontroller

12

與iOS 8,你可以設置標題,而不是消息:

[[[UIAlertView alloc] initWithTitle:@"AlertView in iOS 8." message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]show]; 

UIAlertView中從iOS的8棄用的詳細信息,請訪問該

http://nshipster.com/uialertcontroller/https://developer.apple.com/LIBRARY/IOS/documentation/UIKit/Reference/UIAlertViewDelegate_Protocol/index.html

所以,如果你要編寫單獨的代碼爲iOS 7和iOS 8,你應該使用UIAlertController代替:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"AlertView in iOS 8" message:nil preferredStyle:UIAlertControllerStyleAlert]; 
[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
[self dismissViewControllerAnimated:YES completion:nil]; 
}]]; 
[self presentViewController:alertController animated:YES completion:nil]; 
+1

這不會嘗試回答這個問題。 OP需要一個標題和消息,在iOS 8下使用'UIAlertView'仍然非常好。 – rmaddy 2014-10-07 05:15:55

29

檢查如果類是可用

if ([UIAlertController class]) 
{ 

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Alert title" message:@"Alert message" preferredStyle:UIAlertControllerStyleAlert]; 

    UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]; 
    [alertController addAction:ok]; 

    [self presentViewController:alertController animated:YES completion:nil]; 

} 
else 
{ 

    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"Alert title" message:@"Alert message" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 

    [alert show]; 

} 
1

您可以檢查代碼

if (([[[UIDevice currentDevice] systemVersion] compare:@"8.0" options:NSNumericSearch] == NSOrderedAscending)) 
{ 
    // use UIAlertView 
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:Title message:desc delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
    [alert show]; 
} 
else { 
    // use UIAlertController 
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:Title message:desc preferredStyle:UIAlertControllerStyleAlert]; 
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 

    }]; 
    [alert addAction:okAction]; 
    [self presentViewController:alert animated:YES completion:nil]; 
} 
3

請通過代碼閱讀以下內容以瞭解如何將字段和按鈕添加到警報。

- (IBAction)UIAlertControllerWithActionSheetTextFields:(id)sender { 
 
    UIAlertController * alert= [UIAlertController 
 
            alertControllerWithTitle:@"Info" 
 
            message:@"You are using UIAlertController with Actionsheet and text fields" 
 
            preferredStyle:UIAlertControllerStyleAlert]; 
 
    
 
    UIAlertAction* ok = [UIAlertAction 
 
         actionWithTitle:@"OK" 
 
         style:UIAlertActionStyleDefault 
 
         handler:^(UIAlertAction * action) 
 
         { 
 
          NSLog(@"Resolving UIAlert Action for tapping OK Button"); 
 
          [alert dismissViewControllerAnimated:YES completion:nil]; 
 
           
 
         }]; 
 
    UIAlertAction* cancel = [UIAlertAction 
 
          actionWithTitle:@"Cancel" 
 
          style:UIAlertActionStyleDefault 
 
          handler:^(UIAlertAction * action) 
 
          { 
 
           NSLog(@"Resolving UIAlertActionController for tapping cancel button"); 
 
           [alert dismissViewControllerAnimated:YES completion:nil]; 
 
            
 
          }]; 
 
    
 
    [alert addAction:ok]; 
 
    [alert addAction:cancel]; 
 
    
 
    [alert addTextFieldWithConfigurationHandler:^(UITextField * textField) { 
 
     textField.accessibilityIdentifier = @"usernameTextField"; 
 
     textField.placeholder = @"Enter your username"; 
 
     textField.accessibilityLabel = @"usernameLabel"; 
 
    }]; 
 
    
 
    [alert addTextFieldWithConfigurationHandler:^(UITextField * textField) { 
 
     textField.placeholder = @"Enter your password"; 
 
     textField.accessibilityIdentifier = @"paswordTextField"; 
 
     textField.accessibilityLabel = @"passwordLabel"; 
 
    }]; 
 
    
 
    [self presentViewController:alert animated:YES completion:nil]; 
 
    
 
}

,如果你需要一個項目,完全是指,在IOS中可用的類型的警報,請按照我的項目從以下網址:

Alert variations in IOS

0
let alert = UIAlertController(title: "Warning" as String , message: messageString as String, preferredStyle: .Alert) 
     let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) 
     { 
      UIAlertAction in 
       self.dismissViewControllerAnimated(true, completion: nil) 
     } 
     // Add the actions 
     alert.addAction(okAction) 
     self.presentViewController(alert, animated: true, completion: nil)