2014-06-05 23 views
11

Tôi nhận được lệnh: Không tìm thấy ký hiệu: _OBJC_CLASS _ $ _ UIAlertAction khi tôi cố gắng để có được tính quái dị này để chạy.Cảnh báo có thể hoạt động trên iOS 7 và iOS 8

Làm cách nào để liên kết yếu với 8.0 công cụ?

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; 

Đã giải quyết: UIKit phải được đánh dấu Tùy chọn thay vì Bắt buộc. phiên bản đơn giản bây giờ là:

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

thể trùng lặp của [Lỗi cho thấy một UIAlertView trong nhanh chóng] (http://stackoverflow.com/questions/24040519/error-showing-a-uialertview-in-swift) – Daniel

+1

Đây là một superset của vấn đề bạn đã gọi. Người khác đã gợi ý let cảnh báo = UIAlertView() alert.title = "noop" alert.message = "Không có gì để xác minh" alert.addButtonWithTitle ("Click") alert.show() mà làm việc cho tôi. ai đó chắc chắn sẽ tìm thấy một lỗi với Apple về inti thuận tiện bị hỏng trên UIAlertView shim –

+0

Vui lòng xem câu trả lời tôi đã xuất bản tại đây: [http://stackoverflow.com/a/24091779/1485344][1] [1]: http://stackoverflow.com/a/24091779/1485344 –

Trả lời

5
  • Rõ ràng thêm UIKit trong "Liên kết Binary Với thư viện" của xây dựng giai đoạn của dự án của bạn.

  • Bạn có thể kiểm tra sự tồn tại của UIAlertController như thế này:

    if NSClassFromString("UIAlertController") != nil { 
        // Use it 
    } else { 
        // Fall back 
    } 
    
  • Tôi đã viết một wrapper rằng hoạt động trên cả iOS 7 và iOS 8. Bạn có thể find it here. Phải mất một điều khiển xem tiếp theo là một loạt các đối số tùy chọn và bất kỳ số lượng nút:

    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

tôi chưa thử nghiệm điều này những gì tôi đang lên kế hoạch để làm, một wrapper.dù sao đi nữa, sự khác biệt của kế hoạch của tôi là của bạn là tôi sẽ tạo một trình bao bọc trong ngôn ngữ nhanh nhưng tôi sẽ sử dụng nó trong object-c. –

+0

Điều này có vẻ thú vị. Tuy nhiên, trong khi nó hoạt động hoàn hảo trên iOS8, trên iOS7 nó hiển thị cảnh báo như dự định nhưng nó không gọi trình xử lý hoàn thành cho hai nút. Tôi có thể làm gì sai? showAlert (tabBarController, kiểu: .Alert, tiêu đề: "Yêu cầu đăng nhập", thông báo: "Bạn phải đăng nhập trước khi sử dụng chức năng này.", SourceView: nil, completion: nil, \t (.Cancel, "Cancel" , {println ("ko")}), \t (.DEFAULT, "Đăng nhập", { \t \t println ("ok") \t})) – devguy

+0

tôi đã cố gắng sao chép/dán dụ của riêng bạn, và thậm chí điều đó không 't làm việc cho tôi trên iOS7 – devguy

3

Trong Xcode 6 beta 5, không có gì trong giai đoạn Link là -> Link Binary Với thư viện kể từ khi Swift liên kết khung ngầm. Trong trường hợp này, bạn cần thêm nó theo cách thủ công ở đó và đánh dấu tùy chọn sau đó.

Ngoài ra thay vì kiểm tra các phiên bản hệ điều rõ ràng bạn có thể chỉ cần kiểm tra cho UIAlertController sẵn

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

Hãy thử bên dưới mã cho C. Mục tiêu nó hoạt động tốt cho cả iOS 8 và thấp hơn phiên bản.

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]; 
} 
Các vấn đề liên quan