2014-04-21 71 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 

希望這有助於.. :)

相關問題