从UIView使用presentViewController

我正在为我的iOS应用程序创build一个标题栏,我正在UIView内。 我唯一的问题是“主页button”。 当主页button被按下时,它需要到主页,这是ViewController。

但是,它看起来不像是[self presentViewController:vc animated:NO completion:NULL]; 适用于UIView。

我该如何解决这个问题?

 - (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]; } 

只有UIViewController可以显示另一个视图控制器,所以如果你需要从视图中显示一个视图控制器有几种方法,其中之一是这样的:

使你的父视图所在的视图控制器成为它的代表

 ParentView *view = [ParentView new]; ... view.delegate = self; 

然后,在ParentView中调用该委托的方法

 - (IBAction)homeButtonPressed:(id)sender { [self.delegate buttonPressed]; } 

然后在你的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]; } 

如果你需要在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]; } 

您可以在不使用委托模式的情况下执行此操作。 干得好

的ObjectiveC

 UIViewController *currentTopVC = [self currentTopViewController]; currentTopVC.presentViewController......... - (UIViewController *)currentTopViewController { UIViewController *topVC = [[[[UIApplication sharedApplication] delegate] window] rootViewController]; while (topVC.presentedViewController) { topVC = topVC.presentedViewController; } return topVC; } 

迅速

 var topVC = UIApplication.sharedApplication().keyWindow?.rootViewController while((topVC!.presentedViewController) != nil) { topVC = topVC!.presentedViewController } topVC?.presentViewController........ 

这里有一个比较习惯的Swift 3版本的Shamsudheen的答案:

 extension UIApplication { static func topViewController() -> UIViewController? { guard var top = shared.keyWindow?.rootViewController else { return nil } while let next = top.presentedViewController { top = next } return top } } 

那么你可以打电话给:

 UIApplication.topViewController()?.present(...) 

你可以尝试下面的代码

  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];