2014-12-13 73 views
0

我正在創建一個Singleton來管理我的iCloud設置和配置。iOS:如何在iCloud Singleton中呈現UIAlertController? (Swift)

隨着iCloud的設計指南的部分文件規定: https://developer.apple.com/library/ios/documentation/General/Conceptual/iCloudDesignGuide/Chapters/iCloudFundametals.html

上市1-4邀請用戶使用iCloud的

if (currentiCloudToken && firstLaunchWithiCloudAvailable) { 
UIAlertView *alert = [[UIAlertView alloc] 
         initWithTitle: @"Choose Storage Option" 
           message: @"Should documents be stored in iCloud and 
              available on all your devices?" 
          delegate: self 
        cancelButtonTitle: @"Local Only" 
        otherButtonTitles: @"Use iCloud", nil]; 
[alert show]; 
} 

我在斯威夫特轉換這一個UIAlertController但是我怎樣才能從我的singleton運行self.presentViewController(alert, animated: true, completion: nil)

顯然,我的單身does not have a member named 'presentViewController'

反正有沒有做到這一點還是我需要設置一個郵政通知,提出警報別的地方在我的程序,然後保存/恢復用戶的選擇?

回答

0

1)添加自定義協議委託給你單身,這代表將返回的UIViewController實例

protocol TestSingletonDelegate : NSObjectProtocol { 
    func responderViewControllerForTestSingleton(singleton: TestSingleton)->UIViewController 
} 

而在你的類:

let delegate: TestSingletonDelegate 
func presentAlertView() { 
    let alertView = UIAlertView(); 
    if (self.delegate.respondsToSelector("responderViewControllerForTestSingleton:")) { 
     let viewController = self.delegate.responderViewControllerForTestSingleton(TestSingleton()) 
     viewController.presentViewController(alertView, animated: YES, completion: nil) 
    } 
} 

2)添加例如VAR你的單例類,和故事UIViewController變量那裏

0

我有同樣的問題,我解決了它在我的情況下,只需將alertController返回到單身人士,然後presentin在那裏。

在AlertView I類有一個簡單的警報,可以採取的標題和消息作爲輸入和顯示的只有一個OK按鈕以解除警報下面的代碼:

func alert(#alertTitle: String, alertMessage: String) -> UIAlertController { 

    var alertController = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .Alert) 

    var okButton = UIAlertAction(title: "OK", style: .Default, handler: nil) 

    alertController.addAction(okButton) 

    return alertController 

} 

注意,在上述方法我不是將警報控制器呈現給視圖,而是將其返回給曾經調用該方法的人。

然後在我叫我做下面的代碼的方法的類:

**只是注意,如果我打電話報警的類是Objective-C的一個視圖控制器。我將在下面提供swift代碼以及**

// Creating an instance of the AlertView class and initializing it 
AlertView *testAlert = [[AlertView alloc] init]; 

/* Creating a UIAlertController object and then calling the method I created in the AlertView class. This way the returned UIAlertController will be assigned to this UIAlertController 
*/ 

UIAlertController *alertController = [testAlert alertWithAlertTitle:@"Genius!" alertMessage:@"Pure Genius"]; 


// Finally presenting the alert controller 
[self presentViewController:alertController animated:true completion:nil]; 

注意:我發現您必須在ViewDidLoad之外執行此操作。它在ViewDidLoad中不起作用,並給出以下錯誤:

Attempt to present <UIAlertController: 0x1555118f0> on <ViewController: 0x155509790> whose view is not in the window hierarchy! 

相反,您可以在viewDidAppear方法中執行此操作。

現在斯威夫特視圖控制器的版本:

// Created a function to show the alert 
func showAlert() { 

    // Creating an instance of the AlertView class and initializing it 
    var testAlert : AlertView = AlertView() 

    /* Creating a UIAlertController object and then calling the method I created in the AlertView class. This way the returned UIAlertController will be assigned to this UIAlertController 
    */ 
    var alertController : UIAlertController = testAlert.alert(alertTitle: "Genius", alertMessage: "Pure Genius") 


    // Finally presenting the alert controller 
    self.presentViewController(alertController, animated: true, completion: nil) 
} 

,如果你想顯示在一個Singleton此警報的視圖控制器之外,那麼你可以檢索頂視圖控制器下面,然後提出警告還有一件事控制器到它:

目的 - C:

UIViewController *topViewController = [UIApplication sharedApplication].keyWindow.rootViewController; 

[topViewController presentViewController:alertController animated:true completion:nil]; 

夫特:

if let topViewController = UIApplication.sharedApplication().keyWindow?.rootViewController? { 

     topViewController.presentViewController(alertController, animated: true, completion: nil) 

    } 
0
class SharedDataSingleton { 

    init(){ 

     //println("Hellow World") 
    } 


    var sample = "Hellow word" 

    func showAlertView(title alerTitle:String ,message alertMessage:String, preferredStyle style:UIAlertControllerStyle, okLabel: String, cancelLabel: String, targetViewController: UIViewController,okHandler: ((UIAlertAction!) -> Void)!, cancelHandler: ((UIAlertAction!) -> Void)!){ 

      let alertController = UIAlertController(title: alerTitle, message: alertMessage, preferredStyle: style) 
      let okAction = UIAlertAction(title: okLabel, style: .Default, handler: okHandler) 
      let cancelAction = UIAlertAction(title: cancelLabel, style: .Default,handler: cancelHandler) 

      // Add Actions 
      alertController.addAction(okAction) 
      alertController.addAction(cancelAction) 

      // Present Alert Controller 
      targetViewController.presentViewController(alertController, animated: true, completion: nil) 

    } 





} 

let sharedDataSingletonInstance = SharedDataSingleton() 


> Access the values like below 


sharedDataSingletonInstance.showAlertView(title: "Sample", 
message: "Sample", 
preferredStyle: UIAlertControllerStyle.Alert, 
okLabel: "Ok", 
cancelLabel: "Cancel", 
targetViewController: self, 
okHandler: { (action) -> Void in 
      println("The user is not okay.") 
      }, 
cancelHandler: { (action) -> Void in 
       println("The user is not okay.") 
     }) 


    } 



> Global Variables can be also accessed anywhere in our project 

sharedDataSingletonInstance.sample 
+1

請添加一些(簡單)文字介紹您的答案,謝謝。 – 2015-09-13 15:51:56

相關問題