2016-06-17 71 views
-4

我想讓我的第一個iOS應用程序,和教程要求我使用UIAlertView。 XCode告訴我這已被棄用,我應該使用UIAlertController。我用UIAlertController替換了UIAlertView的實例,並且仍然遇到問題:它說:「沒有可見的@interface for'UIAlertController'聲明選擇器'initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:' 我將不勝感激任何幫助我的代碼如下:IOS/Objective C開發新手; UIAlertView /控制器問題

// 
// ViewController.m 
// cv 
// 
// Created by Frank Michael Tocci on 6/17/16. 
// Copyright © 2016 Frank Tocci. All rights reserved. 
// 

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    UIAlertController * alert = [[UIAlertController alloc] initWithTitle:@"Hello!" message:@"Welcome to OpenCV" delegate:self cancelButtonTitle:@"Continue" otherButtonTitles:nil]; 
    [alert show]; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 
+0

'UIAlertView'和'UIAlertController'是兩個不同的東西。另外,不建議使用'UIAlertView'。 – liamnichols

+1

請閱讀UIalertController的文檔以及如何使用它! –

回答

0

UIAlertController不UIAlertView中

你初始化並顯示它是不同的方式。具體方法如下:

// Init the alert 
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert" 
               message:@"This is an alert." 
               preferredStyle:UIAlertControllerStyleAlert]; 

// Setup the action for the alert 
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" 
               style:UIAlertActionStyleDefault 
               handler:^(UIAlertAction * action) {}]; 

// Add the defaultAction to the alert (this will add an action button with title "OK" to the alert) 
[alert addAction:defaultAction]; 

// Display it 
[self presentViewController:alert animated:YES completion:nil]; 
+0

我不知道爲什麼這會被拒絕,這對我有效 –

+0

可能是因爲所有的用戶都做了蘋果公司的例子,而不是僅僅解釋差異和鏈接到Apple文檔。 –

2

UIAlertController比UIAlertView中不同的接口,必須使用不同的構造函數,它在短短的標題,信息和喜歡的風格

然後你就可以添加動作。一旦定義了UIAlertController,點擊超鏈接,它會帶你到蘋果的文檔,他們也提供了一個n如何使用UIAlertController的例子。

0

如果要部署爲iOS 8應用程序,然後只需更改部署目標到iOS 8在這之後,你不會找到任何折舊使用AlertViewAlertView在iOS 9及更高版本中折舊,但低於iOS 9時仍能正常工作。