【发布时间】:2018-08-19 13:24:06
【问题描述】:
我有一个位于标签栏内的表格视图。当我单击表格视图时,它会显示一个弹出视图控制器,如下所示:
但是,每当我导航到另一个标签栏时,弹出视图保持原样而不关闭它,当我回到上一个弹出视图时,它会显示一个深色背景,如下图所示:
我该如何解决这个问题?
*注意 = 我是 IOS 新手
添加子视图的代码:
import UIKit
class NotificationViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
var dataALL = [NotificationDataModel]()
@IBOutlet weak var tableViewAll: UITableView!
@IBOutlet var mainSegmentedControl: UISegmentedControl!
lazy var iNotificationViewController : INotificaitonViewController = {
let storyBoard = UIStoryboard(name: "Main", bundle: Bundle.main)
var viewController = storyBoard.instantiateViewController(withIdentifier: "INotificaitonViewController") as! INotificaitonViewController
self.addViewContollerAsChildViewcontroller(childViewContoller: viewController)
return viewController
}()
var activityIndicator : UIActivityIndicatorView = UIActivityIndicatorView()
override func viewDidLoad() {
super.viewDidLoad()
tableViewAll.delegate = self
tableViewAll.dataSource = self
let nib = UINib(nibName: "TableViewCell", bundle: nil)
tableViewAll.register(nib, forCellReuseIdentifier: "customCell")
setupView()
tableViewAll.separatorStyle = .none
self.startShowingIndication()
downloadAllJSONData {
self.tableViewAll.reloadData()
self.stopShowingIndication()
print("sucess")
}
}
func downloadAllJSONData(completed : @escaping ()->()){
guard let url = URL(string: "http://www.swipecrafts.com/notice/get")else {return}
var request = URLRequest.init(url: url)
request.httpMethod = "POST"
request.addValue("cf7ab8c9d4efae82b575eabd6bec76cbb86c6108391e036387f3d5356a582171519367747000", forHTTPHeaderField: "api_ky")
let postdata = "school_id=\(SCHOOL_ID)"
request.httpBody = postdata.data(using: .utf8)
URLSession.shared.dataTask(with: request) { (data, response, error) in
if error == nil{
do{
self.dataALL = try JSONDecoder().decode([NotificationDataModel].self, from: data!)
DispatchQueue.main.async {
completed()
print(self.dataALL.count)
}
}catch{
print(error)
}
}
}.resume()
}
private func setupView(){
setupSegmentedControl()
updateView()
}
private func updateView(){
iNotificationViewController.view.isHidden = !(mainSegmentedControl.selectedSegmentIndex == 0)
}
private func setupSegmentedControl(){
mainSegmentedControl.removeAllSegments()
mainSegmentedControl.insertSegment(withTitle: "General Notification", at: 0, animated: false)
mainSegmentedControl.insertSegment(withTitle: "All Notificatoin", at: 1, animated: false)
mainSegmentedControl.addTarget(self, action: #selector(selectionDidChange(sender:)), for: .valueChanged)
mainSegmentedControl.selectedSegmentIndex = 0
}
@objc func selectionDidChange(sender: UISegmentedControl) {
updateView()
}
private func addViewContollerAsChildViewcontroller (childViewContoller : UIViewController){
addChildViewController(childViewContoller)
view.addSubview(childViewContoller.view)
childViewContoller.view.frame = view.bounds
childViewContoller.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
childViewContoller.didMove(toParentViewController: self)
}
private func removeViewControllerAsChildViewController(childViewController : UIViewController){
childViewController.willMove(toParentViewController: nil)
childViewController.view.removeFromSuperview()
childViewController.removeFromParentViewController()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataALL.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableViewAll.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! TableViewCell
cell.customInit(noticeTitle: dataALL[indexPath.row].notice_title!,noticeDiscripetion: dataALL[indexPath.row].notice_description!)
let downloadedimage = dataALL[indexPath.row].image_name
if downloadedimage != nil{
cell.lblImageNotificaiton.downloadedFrom(link: downloadedimage!)
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "goToPopUpViewww", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? PopUpViewController{
destination.notificationfianlData = dataALL[(tableViewAll.indexPathForSelectedRow?.row)!]
}
}
func startShowingIndication(){
activityIndicator.center = self.view.center
activityIndicator.hidesWhenStopped = true
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
view.addSubview(activityIndicator)
activityIndicator.startAnimating()
}
func stopShowingIndication(){
activityIndicator.stopAnimating()
}
}
【问题讨论】:
-
您的弹出窗口是常规 UIViewController 子类吗?
-
是的,它是@LGP。
-
你能分享一些代码吗?
-
@JenJose 我已经添加了我的代码,请您检查一下!
-
我有一个想法可以解决这个问题,但首先要说一下如何打开弹出窗口? [1] UIView 或 [2] UIViewcontroller
标签: ios uitableview uiviewcontroller popup uitabbarcontroller