ios – 从UIView使用presentViewController
发布时间:2020-12-15 01:53:57 所属栏目:百科 来源:网络整理
导读:我正在为我的iOS应用程序创建一个标题栏,我正在使用UIView。我唯一遇到的问题是“主页按钮”。当按住主页按钮时,需要转到主页,即ViewController。 但是,它看起来不像[self presentViewController:vc animated:NO completion:NULL];为UIView工作。 如
|
我正在为我的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];
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
