2017-04-04 71 views
0

如何創建一個通用的彈出視圖控制器,該控制器可以被具有不同數據的多個視圖控制器調用。 我創建了一個帶有標籤和按鈕的popupviewcontroller類。 根據來自不同視圖控制器的呼叫,標籤和按鈕將具有不同的文本。 總之什麼是創建可以由多個viewcontrollers如何在swift iOS中創建通用的彈出視圖控制器

class CustomPopUpViewController: UIViewController{ 


@IBOutlet weak var vWCustomSubVw: UIView! 
@IBOutlet weak var lblHeadingText: UILabel! 
@IBOutlet weak var lblDescription: UILabel! 
@IBOutlet weak var btnCustom: UIButton! 

var strLblHeadingText = String() // string for heading label 
var strLblDescription = String() // string for description label 
var strBtnCustom = String()// string for custom button 

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.view.backgroundColor = UIColor.black.withAlphaComponent(0.2) 

    self.showAnimate() 



    lblHeadingText.text = strLblHeadingText 
    lblDescription.text = strLblDescription 
    btnCustom .setTitle(strBtnCustom, for: UIControlState.normal) 

} 

func showAnimate() 
{ 
    self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3) 
    self.view.alpha = 0.0; 
    UIView.animate(withDuration: 0.25, animations: { 
     self.view.alpha = 1.0 
     self.view.transform = CGAffineTransform(scaleX: 1.0, y: 1.0) 
    }); 
} 


func removeAnimate() 
{ 
    UIView.animate(withDuration: 0.25, animations: { 
     self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3) 
     self.view.alpha = 0.0; 
    }, completion:{(finished : Bool) in 
     if (finished) 
     { 
      self.view.removeFromSuperview() 
     } 
    }); 
} 


} 

,我這樣從另一個視圖控制器調用它使用一個通用的彈出觀點正確和可行的方法: -

func btnInfoTapped(){ 

    let customPopUpVC = UIStoryboard(name: "Course", bundle: nil).instantiateViewController(withIdentifier: "CustomPopUpViewController") as! CustomPopUpViewController 
    self.addChildViewController(customPopUpVC) 
    customPopUpVC.view.frame = self.view.frame 
    self.view.addSubview(customPopUpVC.view) 
    customPopUpVC.didMove(toParentViewController: self) 



} 

所以我想說它是通用的,說它是一個全球性的方法或從不同的視圖控制器調用同一類的東西

+1

歡迎SO此功能。你會發現如果你添加你的代碼的例子,你通常會得到更好的答案。即使代碼寫得不好。你說你已經創建了一個班級,添加這個例子。 – BWMustang13

回答

1

您可以創建一個通用方法來呈現警報控制器

import Foundation 
import UIKit 

class AlertHelper: NSObject { 

//Alert with title and dismiss button only 
static func showAlertWithTitle(_ conroller: UIViewController, title: String, message: String = "" ,dismissButtonTitle: String, dismissAction:@escaping()->Void) { 

    let validationLinkAlert = UIAlertController(title:title, message:message, preferredStyle: .alert) 
    let dismissAction = UIAlertAction(title: dismissButtonTitle, style: .default) { (action) -> Void in 
     dismissAction() 
    } 

    validationLinkAlert.addAction(dismissAction) 
    conroller.present(validationLinkAlert, animated: true, completion: nil) 
} 

//Alert with title with message 
static func showALertWithTitleAndMessage(_ controller: UIViewController, title: String, message: String, dismissButtonTitle: String, okButtonTitle: String, dismissAction:@escaping()-> Void, okAction:@escaping()-> Void) { 

    let validationLinkAlert = UIAlertController(title:title, message:message, preferredStyle: .alert) 

    let dismissAction = UIAlertAction(title: dismissButtonTitle, style: UIAlertActionStyle.default) { (action) in 
     dismissAction() 
    } 

    let okAction = UIAlertAction(title: okButtonTitle, style: UIAlertActionStyle.default) { (action) in 
     okAction() 
    } 

    validationLinkAlert.addAction(dismissAction) 
    validationLinkAlert.addAction(okAction) 

    controller.present(validationLinkAlert, animated: true, completion: nil) 

} 
} 

呼叫從您的視圖控制器

AlertHelper.showAlertWithTitle(self, title: message, dismissButtonTitle: "OK") {() -> Void in 

    } 
+0

要求是讓自定義用戶界面 – mirza

+0

有很多開源可用的用戶或者通過靈感創建自己的用戶界面。檢查這一個https://github.com/Orderella/PopupDialog –

相關問題