iOS   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 从UIView使用presentViewController大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_489_1@

概述

我正在为我的iOS应用程序创建一个标题栏,我正在使用UIView。我唯一遇到的问题是“主页按钮”。当按住主页按钮时,需要转到主页,即ViewController。 但是,它看起来不像[self presentViewController:vc animated:NO completion:NULL];为UIView工作。 如何解决这个问题? - (void) createtitlebar {
我正在为我的iOS应用程序创建一个标题栏,我正在使用UIView。我唯一遇到的问题是“主页按钮”。当按住主页按钮时,需要转到主页,即ViewController。

但是,它看起来不像[self presentViewController:vc animated:NO completion:NULL];为UIView工作。

如何解决这个问题?

- (void) createtitlebar {

    CGRect titleBarFrame = CGRectMake(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可以呈现另一个视图控制器,所以如果你需要从视图中显示一个viewcontroller,有几种方式,其中之一就是这样:

一个视图控制器,您的父视图位于该视图的代表处

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

大佬总结

以上是大佬教程为你收集整理的ios – 从UIView使用presentViewController全部内容,希望文章能够帮你解决ios – 从UIView使用presentViewController所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。