2014-06-05 36 views
11

我得到dyld:找不到符號:_OBJC_CLASS _ $ _ UIAlertAction 當我試圖讓這個怪物運行。警報,可以在iOS 7和iOS 8上工作

如何弱化8.0的東西?

var device : UIDevice = UIDevice.currentDevice()!; 
var systemVersion = device.systemVersion; 
var iosVerion : Float = systemVersion.bridgeToObjectiveC().floatValue; 
if(iosVerion < 8.0) { 
    let alert = UIAlertView() 
    alert.title = "Noop" 
    alert.message = "Nothing to verify" 
    alert.addButtonWithTitle("Click") 
    alert.show() 
} else { 
    var alert : UIAlertController? = UIAlertController(title: "Noop", message: "Nothing to verify", preferredStyle: UIAlertControllerStyle.Alert) 
    if alert { 
     let actionStyle : UIAlertActionStyle? = UIAlertActionStyle.Default; 
     var alertAction : UIAlertAction? = UIAlertAction(title: "Click", style: actionStyle!, handler: nil) 
     if(alertAction) { 
      alert!.addAction(alertAction) 
      self.presentViewController(alert, animated: true, completion: nil) 
     } 
    } 
} 
return; 

解決:UIKit必須標記爲「可選」而非「必需」。現在簡化版本是:

var device : UIDevice = UIDevice.currentDevice()!; 
var systemVersion = device.systemVersion; 
var iosVerion : Float = systemVersion.bridgeToObjectiveC().floatValue; 
if(iosVerion < 8.0) { 
    let alert = UIAlertView() 
    alert.title = "Noop" 
    alert.message = "Nothing to verify" 
    alert.addButtonWithTitle("Click") 
    alert.show() 
} else { 
    var alert : UIAlertController = UIAlertController(title: "Noop", message: "Nothing to verify", preferredStyle: UIAlertControllerStyle.Alert) 
    alert.addAction(UIAlertAction(title: "Click", style:.Default, handler: nil)) 
    self.presentViewController(alert, animated: true, completion: nil) 
} 
+0

的[錯誤表示迅速一個UIAlertView中](http://stackoverflow.com/questions/24040519/error-showing-a-uialertview-in-swift) – Daniel

+1

這可能重複的是該問題的一個超集你已經提到。有人曾建議 設警報= UIAlertView中() alert.title = 「NOOP」 alert.message = 「無驗證」 alert.addButtonWithTitle( 「點擊」) alert.show() 這工作了我。有人應該肯定找到一個與蘋果有關方便inti破壞UIAlertView墊片的錯誤 –

+0

請看看我在這裏發表的答案: [http://stackoverflow.com/a/24091779/1485344][1] [1]:http://stackoverflow.com/a/24091779/1485344 –

回答

5
  • 明確項目的構建階段的「鏈接二進制與圖書館」一節中添加的UIKit。

  • 可以測試UIAlertController存在這樣的:

    if NSClassFromString("UIAlertController") != nil { 
        // Use it 
    } else { 
        // Fall back 
    } 
    
  • 我寫了在iOS 7和iOS 8.您可以find it here都工作的包裝。它需要一個視圖控制器隨後一堆可選參數和任意數量的按鈕:

    showAlert(self, style: .ActionSheet, sourceView: cell, completion: { 
        tableView.deselectRowAtIndexPath(indexPath, animated: true) 
    }, 
        (.Default, "Send clipboard", { 
         if someCondition { 
          // completion must be specified because of a Swift bug (rdar://18041904) 
          showAlert(self, title: "Nothing to send", message: "The clipboard is empty.", completion: nil, 
           (.Cancel, "OK", nil) 
          ) 
         } 
        }), 
        (.Cancel, "Cancel", nil) 
    ) 
    
+0

我還沒有測試過這個,但這正是我計劃要做的,一個包裝,贊成這個努力。無論如何,我的計劃與你的不同之處在於我會用swift語言創建一個包裝器,但是我會在object-c中使用它。 –

+0

這看起來很酷。然而,雖然它在iOS8上完美工作,但在iOS7上它按預期顯示了alertview,但它沒有調用兩個按鈕的完成處理程序。我能做什麼錯了? showAlert(tabBarController,style:.Alert,title:「需要登錄」,消息:「您必須先登錄才能使用此函數。」,sourceView:nil,completion:nil, \t(.Cancel,「Cancel」 {的println( 「KO」)}), \t(.DEFAULT, 「登錄」,{ \t \t的println( 「OK」) \t})) – devguy

+0

我試圖複製/粘貼自己的例子,甚至說沒有按在iOS7上不適合我 – devguy

3

在Xcode中6測試版5,有沒有在鏈路階段 - >自斯威夫特鏈接二進制與圖書館隱含鏈接框架。在這種情況下,您需要手動將其添加並標記爲可選。

而且不是檢查系統版本明確你可以檢查UIAlertController可用性

if nil != NSClassFromString("UIAlertController") { 
    //show alertcontroller 
    ... 
} else { 
    //show alertview 
    ... 
} 
-1

嘗試以下爲目的C.代碼它工作正常的不論是iOS 8和下面的版本。

if (IS_OS_8_OR_LATER) { 
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert]; 

UIAlertAction *cancelAction = [UIAlertAction 
          actionWithTitle:@"OK" 
          style:UIAlertActionStyleCancel 
          handler:^(UIAlertAction *action) 
          { 

          }]; 
[alertVC addAction:cancelAction]; 

[[[[[UIApplication sharedApplication] windows] objectAtIndex:0] rootViewController] presentViewController:alertVC animated:YES completion:^{ 

}]; 
} 
else{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
    [alert show]; 
} 
相關問題