我正在使用这个objective-c代码向窗口添加一个视图:
// Main Storyboard
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
// View controller with the view to add
UIViewController *vc = [mainStoryBoard instantiateViewControllerWithIdentifier:@"SomeViewController"];
// Find the topmost window
UIWindow *topWindow = [[[UIApplication sharedApplication].windows sortedArrayUsingComparator:^NSComparisonResult(UIWindow *win1, UIWindow *win2) {
return win1.windowLevel < win2.windowLevel || !win1.isOpaque;
}] lastObject];
// Add the view to the window
[topWindow addSubview:vc.view];
这很好用,我SomeViewController
用来离开视图的这个 Swift 方法也是如此:
// Get topmost window (Should be UIWindow)
let topWindow = UIApplication.shared.windows.sorted {
(win1, win2) in
return win1.windowLevel < win2.windowLevel || !win1.isOpaque
}.last
// Force view to prepare to disappear
self.viewWillDisappear(true)
// Send the view to the back
topWindow?.sendSubviewToBack(view)
这是我的objective-c代码运行后的视图结构。所选视图是添加的视图:
问题是新视图中的按钮都不起作用,因此我无法触发上述关闭方法。我已经尝试了使用情节提要添加的按钮和以编程方式添加的按钮。视觉上,按钮在点击时会做出反应,但它们不会触发任何东西。