ios - 单击按钮删除 View

标签 ios swift

我正在使用以下代码通过集合加载我的表。

self.window = UIWindow(frame: CGRect(x:40, y:120, width:self.view.bounds.width-100, height:self.view.bounds.height-200))
self.window!.backgroundColor = UIColor.whiteColor()

let mainController : CollectionViewController = CollectionViewController(nibName: "CollectionViewController", bundle: nil)
myController.TableHeaderArray = TableHeaders
let ApiResponse = self.dataTableViewdictParams(apiUrl)
 myController.UMIDDataArray = ApiResponse[0] as! NSMutableArray
myController.TableDataArray = ApiResponse[1] as! NSMutableArray
self.window!.rootViewController = myController
self.window!.makeKeyAndVisible()

我想在单击按钮时删除此表并加载另一个 View 。单击按钮时会加载另一个 View ,但我无法删除此表 UIWindowView。我怎样才能删除这个?

编辑:

添加了我尝试过的代码:

function loadanotherView()
{
/// I tried these three codes but none worked for me
self.window?.removeFromSuperview() // code 1
self.window?.rootViewController?.removeFromParentViewController() // code 1
self.window?.hidden = true // code 2

let frame = CGRect(x:0, y:-20, width:self.view.bounds.width, height:self.view.bounds.width)
WView.frame=frame
let url = NSURL(string:"urlForTheWebView")
let req = NSURLRequest(URL:url!)
self.webView!.loadRequest(req)
self.view.addSubview(WView)

} 

最佳答案

不要使用

self.window!.rootViewController = myController

使用这个

self.window?.addSubview(myController.view)

而不是使用

self.window?.removeFromSuperview() // code 1
self.window?.rootViewController?.removeFromParentViewController() // code 1
self.window?.hidden = true // code 2

使用这个,

myController.view.removeFromSuperview() // If Button on the another viewcontroller

self.view.removeFromSuperview() // If Button on the myController

因为如果你将 myController 设置为 root,它就无法从 superView 中删除。因此将其添加为 subview 。

关于ios - 单击按钮删除 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40153637/

相关文章:

ios - 在 Swift 中循环遍历 Realm 对象字段

ios - 在 UITableViewCell 中逐行显示字符串的动画

ios - UITableView 单元格内容在 iOS 9 上加载正常,但在 iOS 8 上加载不正常

ios - 在将对象传递给它之前需要一个 View Controller 存在

iphone - 在Appdelegate类中控制一个类的AVAudioPlayer

ios - TableView 选中标记和取消选中并向上滚动仍然选中 Ios Swift 4 中的单元格值

ios - UITextView .isEmpty 不适用于键盘发送 Swift

swift - 协议(protocol)实现 init 的访问控制

ios - 使用 swift 放大到 PDFKit 中的特定坐标

swift - swift 中的 'let' 是什么意思?