2016-11-11 3 views
1

swift3の新機能です。今、私はこのような他のswift.fileswift3コールアラート機能他swift.file

からのアラート機能を呼び出すために方法を見つけるしています:

//MainView.swift 
//Call function 
AlertFun.ShowAlert(title: "Title", message: "message...") 

//Another page for storing functions 
//Function.swift 

public class AlertFun { 
    class func ShowAlert(title: String, message: String) {  
     let alert = UIAlertController(title: tile, message: message, preferredStyle: UIAlertControllerStyle.alert) 
     alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil)) 
     self.present(alert, animated: true, completion: nil) 
    } 
} 

ここでの問題... ...このような方法でこれを行うことはできません

self.present(alert, animated: true, completion: nil) 

どうすれば実装できますか?ありがとう。

答えて

4

ようshowAlert関数のパラメータとしてのViewController参照を渡す:あなたのコントローラの

//MainView.swift 
//Call function 
AlertFun.ShowAlert(title: "Title", message: "message...", in: self) 

//Another page for storing functions 
//Function.swift 

public class AlertFun { 
    class func ShowAlert(title: String, message: String, in vc: UIViewController) {  
     let alert = UIAlertController(title: tile, message: message, preferredStyle: UIAlertControllerStyle.alert) 
     alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil)) 
     vc.present(alert, animated: true, completion: nil) 
    } 
} 
+0

。ありがとう –

1

コール方法

Utility.showAlertOnViewController(targetVC: self, title: "", message:"") 

あなたのクラスまあテスト

class Utility: NSObject { 
    class func showAlertOnViewController(
      targetVC: UIViewController, 
      title: String, 
       message: String) 
     { 

      let alert = UIAlertController(
       title: title, 
       message: message, 
       preferredStyle: UIAlertControllerStyle.alert) 
      let okButton = UIAlertAction(
       title:"OK", 
       style: UIAlertActionStyle.default, 
       handler: 
       { 
        (alert: UIAlertAction!) in 
      }) 
      alert.addAction(okButton) 
      targetVC.present(alert, animated: true, completion: nil) 
     } 
} 
関連する問題