使用UIView中的presentViewController [英] Using presentViewController from UIView
问题描述
我正在为我的iOS应用程序创建一个标题栏,我在UIView中创建它。我唯一的问题是主页按钮。按下主页按钮时,需要转到主页,即ViewController。
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.
然而,它看起来不像 [self presentViewController:vc animated:NO completion:NULL];
适用于UIView。
However, it doesn't look as if [self presentViewController:vc animated:NO completion:NULL];
works for 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可以呈现另一个视图控制器,所以如果你需要从视图中显示一个viewcontroller有几种方法,其中一种是这样的:
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:
制作一个你父视图所在的viewcontroller代表它
make a viewcontroller on which your parent view is situated a delegate of it
ParentView *view = [ParentView new];
...
view.delegate = self;
然后,在该委托的ParentView调用方法中
then, inside ParentView call method of that delegate
- (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中并避免委派,您可以执行像这样的伎俩(我个人不喜欢它,但它应该工作)
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)
-(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];
}
这篇关于使用UIView中的presentViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!