gpt4 book ai didi

ios - 如何以横向模式打开 View Controller ?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:18:24 27 4
gpt4 key购买 nike

我有一个 TableView ,当在表中点击单元格时,我正在推送另一个 View Controller 如下:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
PriceChart *vc = [storyboard instantiateViewControllerWithIdentifier:@"pricechart"];
[self.navigationController pushViewController:vc animated:YES];

}

现在我的问题是:我要展示的 View Controller 应该处于横向模式,但它处于纵向模式。

另一个问题是当点击不同的单元格时如何打开不同的 View Controller 。我尝试使用 indexpath.row 但还有其他方法可以使用 storyboard 吗?

最佳答案

shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation

如果上述方法位于任何 UINavigationController 中,则它们不会被 UIViewController 调用。如果这些方法在 UINavigationController 中声明,那么它们将被调用。

为了解决这个问题,我们创建了一个类OrientationEnabledNavigation,它是UINavigationController的子类。然后在 OrientationEnabledNavigation 中实现了 shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation 方法。

OrientationEnabledNavigation.h

#import <UIKit/UIKit.h>

@interface OrientationEnabledNavigation : UINavigationController

@end

OrientationEnabledNavigation.m

#import "OrientationEnabledNavigation.h"

@interface OrientationEnabledNavigation ()

@end

@implementation OrientationEnabledNavigation

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (BOOL)shouldAutorotate
{
return [self.topViewController shouldAutorotate];
// return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.topViewController preferredInterfaceOrientationForPresentation];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

然后如果你想在你的项目中使用导航 Controller 然后使用OrientationEnabledNavigation。之后,如果您在 View Controller 中实现 shouldAutorotate、supportedInterfaceOrientations、preferredInterfaceOrientationForPresentation 这些方法,那么它们将被调用。

在你的 viewcontroller 中实现这些:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(BOOL)shouldAutorotate
{
return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}

另一种方法是在您的应用程序委托(delegate)的开头添加一些代码:

对于 UITabBarController

@implementation UITabBarController (AutoRotationForwarding)

-(BOOL)shouldAutorotate
{
if ([self.selectedViewController respondsToSelector:@selector(shouldAutorotate)]) {
return [self.selectedViewController shouldAutorotate];
}
else {
return YES;
}
}

- (NSUInteger)supportedInterfaceOrientations {
if ([self.selectedViewController respondsToSelector:@selector(supportedInterfaceOrientations)]) {
return [self.selectedViewController supportedInterfaceOrientations];
}
else {
return UIInterfaceOrientationMaskAll;
}
}

@end

对于 UINavigationController

@implementation UINavigationController (AutoRotationForwarding)

-(BOOL)shouldAutorotate
{
if ([self.topViewController respondsToSelector:@selector(shouldAutorotate)]) {
return [self.topViewController shouldAutorotate];
}
else {
return YES;
}
}

-(NSUInteger) supportedInterfaceOrientations {
if([self.topViewController respondsToSelector:@selector(supportedInterfaceOrientations)])
{
return [self.topViewController supportedInterfaceOrientations];
}
return UIInterfaceOrientationMaskPortrait;
}

@end

希望这会有所帮助..:)

关于ios - 如何以横向模式打开 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23194036/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com