2014-04-21 74 views
3

我有一个表视图,当细胞表被窃听,我推着另一个视图控制器 如下:如何在横向模式下打开视图控制器?

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

回答

0

这是你可以强制景观

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

-(BOOL)shouldAutorotate 
{ 
    return NO; 
} 

-(NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskPortrait; 
} 
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]; 
} 
0

可以强制取向刷新为解释here并在这一问题的答案。

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

+0

我尝试过,但通过这种方式,我可以显示所有的细胞相同的控制器。 – yashwanth777

+0

你的意思是你可以推动或者你不能? – Rivera

1
shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation 

上述方法不会被调用一个UIViewController的,如果他们是任何UINavigationController内。如果这些方法在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 

然后,如果你想在你的项目中使用导航控制器,然后使用OrientationEnabledNavigation。之后,如果您在视图控制器中实现了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 

希望这有助于.. :)

相关问题