如何在横向模式下打开视图控制器?

时间:2014-04-21 08:55:21

标签: ios objective-c uitableview uiviewcontroller

我有一个表格视图,当在表格中点击单元格时,我正在推动另一个视图控制器 如下:

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

 }

现在我的问题是:我要展示的视图控制器应该处于景观模式但是处于纵向模式。

另一个问题是如何在点击不同的单元格时打开不同的视图控制器。我是使用indexpath.row尝试过的,但有没有其他方式使用storyboard

5 个答案:

答案 0 :(得分:1)

shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation

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

为了解决这个问题,一个类可以调用它OrientationEnabledNavigation,它是UINavigationController的子类。然后在shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation内实施了OrientationEnabledNavigation个方法。

<强> 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

然后,如果您想在项目中使用导航控制器,请使用OrientationEnabledNavigation。之后如果你在viewcontroller中实现shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation这些方法,那么它们就会被调用。

你在viewcontroller中实现这些:

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

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

另一种方法是添加代理开头的代码:

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

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

答案 1 :(得分:0)

这是你强迫横向

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

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

答案 2 :(得分:0)

在横向模式的课程中:

- (NSUInteger)supportedInterfaceOrientations
{
// return orientation that you want
//return UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskPortraitUpsideDown;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
//return UIInterfaceOrientationIsPortrait(toInterfaceOrientation);
}

如果你使用UINavigationController,你应该从UINavigationController继承你的navigationController并实现

- (BOOL)shouldAutorotate
{
return YES;
}

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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return [[self.viewControllers lastObject] shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
}

答案 3 :(得分:0)

试试这个...... 这个演示解决了我的问题......

https://github.com/alloy/ForceOrientationTest

答案 4 :(得分:0)

您可以按照here说明强制进行方向更新,并在该问题中强制进行其他回答。

至于第二个问题,只需用静态单元格或动态原型单元格填充UITableViewController。然后控制+从单元格拖动到Storyboard中的另一个视图控制器。您将拥有显示按钮的相同推送/模态/自定义操作。