ios 使用 UIView 中的presentViewController
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15622736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using presentViewController from UIView
提问by Ashish Agarwal
I am creating a title bar for my iOS application and I am making this inside a UIView. The only issue I am having is with the "home button". When the home button is pressed, it needs to go to the home page, which is ViewController.
我正在为我的 iOS 应用程序创建一个标题栏,并在 UIView 中制作它。我遇到的唯一问题是“主页按钮”。当home键被按下时,需要跳转到首页,也就是ViewController。
However, it doesn't look as if [self presentViewController:vc animated:NO completion:NULL];
works for UIView.
但是,它看起来似乎不适[self presentViewController:vc animated:NO completion:NULL];
用于 UIView。
How do I work around this problem ?
我如何解决这个问题?
- (void) createTitlebar {
CGRect titleBarFrame = CGRectMake(0, 0, 320, 55);
//Create the home button on the top right of the page. When clicked, it will navigate to the home page of the application
homeButton = [UIButton buttonWithType:UIButtonTypeCustom];
homeButton.frame = CGRectMake(275, 10, 40, 40);
[homeButton setTag:1];
[homeButton setImage:[UIImage imageNamed:@"homeIcon.png"] forState:UIControlStateNormal];
[homeButton addTarget:self action:@selector(homeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:homeButton];
}
- (IBAction)homeButtonPressed:(id)sender{
//Transition to the submit button page
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
ViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:vc animated:NO completion:NULL];
}
采纳答案by alex
Only UIViewController can present another view controller, so if you need to show a viewcontroller from view there are several way, one of them is like this:
只有 UIViewController 可以呈现另一个视图控制器,所以如果你需要从视图中显示一个视图控制器,有几种方式,其中一种是这样的:
make a viewcontroller on which your parent view is situated a delegate of it
使您的父视图所在的视图控制器成为它的委托
ParentView *view = [ParentView new];
...
view.delegate = self;
then, inside ParentView call method of that delegate
然后,在该委托的 ParentView 调用方法中
- (IBAction)homeButtonPressed:(id)sender {
[self.delegate buttonPressed];
}
and then, inside your VC implement
然后,在您的 VC 工具中
-(void)buttonPressed {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
ViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:vc animated:NO completion:NULL];
}
If you need to keep this code inside UIView and avoid delegation you can do a trick like this (personally i don't like it but it should work)
如果您需要将此代码保留在 UIView 中并避免委托,您可以执行这样的技巧(我个人不喜欢它,但它应该可以工作)
-(void)buttonPressed{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
ViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[(UIViewController*)self.nextResonder presentViewController:vc animated:NO completion:NULL];
}
回答by Shamsudheen TK
You can do this without using the delegate pattern. Here you go
您可以在不使用委托模式的情况下执行此操作。干得好
ObjectiveC
目标C
UIViewController *currentTopVC = [self currentTopViewController];
currentTopVC.presentViewController.........
- (UIViewController *)currentTopViewController {
UIViewController *topVC = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
while (topVC.presentedViewController) {
topVC = topVC.presentedViewController;
}
return topVC;
}
Swift
迅速
var topVC = UIApplication.sharedApplication().keyWindow?.rootViewController
while((topVC!.presentedViewController) != nil) {
topVC = topVC!.presentedViewController
}
topVC?.presentViewController........
回答by Hans Brende
Here's a more idiomatic Swift 3 version of Shamsudheen's answer:
这是Shamsudheen答案的更惯用的Swift 3版本:
extension UIApplication {
static func topViewController() -> UIViewController? {
guard var top = shared.keyWindow?.rootViewController else {
return nil
}
while let next = top.presentedViewController {
top = next
}
return top
}
}
Then you can just call:
然后你可以打电话:
UIApplication.topViewController()?.present(...)
回答by N. Der
With Swift 4 - 4.2Adding onto Shamsudheen TK's answer.
使用 Swift 4 - 4.2添加到 Shamsudheen TK 的答案中。
var topVC = UIApplication.shared.keyWindow?.rootViewController
while((topVC!.presentedViewController) != nil) {
topVC = topVC!.presentedViewController
}
let customViewController = CustomViewController()
topVC?.present(customViewController, animated: true, completion: nil)
With UINavigationController:Here is also an additional feature -> You can pass along a UINavigationController with your customViewController.
使用 UINavigationController:这里还有一个附加功能 -> 您可以使用 customViewController 传递 UINavigationController。
var topVC = UIApplication.shared.keyWindow?.rootViewController
while((topVC!.presentedViewController) != nil) {
topVC = topVC!.presentedViewController
}
let customViewController = CustomViewController()
let navController = UINavigationController(rootViewController: CustomViewController)
topVC?.present(navController, animated: true, completion: nil)
回答by Hardik Thakkar
You can try below code
你可以试试下面的代码
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
ViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
UIViewController *vc1 = [UIApplication sharedApplication].keyWindow.rootViewController;
[vc1 presentViewController:vc animated:YES completion:nil];