如何在Master-Detail应用程序中加载完全不同的ViewController?

问题描述:

我有一个主 - 细节应用程序,我想加载一个新的视图,在主点击行。如果我选择第1行,则视图不同,并且在选择不同的行时,新视图将显示在Detail部分中。我怎样才能做到这一点?如何在Master-Detail应用程序中加载完全不同的ViewController?

我正在做这样的事情。问题是 - 如果我第一次选择这两行中的任何一行,视图实际上会改变。但是,当我点击另一行时,它不会改变。

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

if(indexPath.row==0){ 


    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 

    TwitterTweetViewController *detailView=(TwitterTweetViewController *)[storyboard instantiateViewControllerWithIdentifier:@"TwitterDetailView"]; 

    self.splitViewController.mainViewController = detailView; 
    [self.splitViewController.mainViewController viewDidLoad]; 
    [self.splitViewController viewDidLoad]; 

} 


if(indexPath.row == 1) 
{ 

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 

    TwitterDetailViewController *detailView=(TwitterDetailViewController *)[storyboard instantiateViewControllerWithIdentifier:@"TwitterDetailView"]; 

    self.splitViewController.mainViewController = detailView; 
    [self.splitViewController.mainViewController viewDidLoad]; 
    [self.splitViewController viewDidLoad]; 

} 

}

我应该为了改变选择这些行的看法呢?

+0

这是你在找什么? http://*.com/questions/9272244/uisplitviewcontroller-on-ipad-with-storyboards – 2012-05-15 02:47:37

有一个伟大的方法:

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

使用它。它是在单元格点击完成后调用的。你可以在那里放一些支票。简单的例子:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (indexPath.row == 0) { 
    SomeViewController *svc = [[SomeViewController alloc]initWithNibName:@"SomeViewController" bundle:nil]; 
    [self.navigationController pushViewController:svc]; 
    } else if (indexPath.row > 0 && indexPath.row < 5) { 
    SomeOtherViewController *sovc = [[SomeOtherViewController alloc]initWithNibName:@"SomeOtherViewController" bundle:nil]; 
    [self.navigationController pushViewController:sovc]; 
    } else { 
    AnotherViewController *avc = [[AnotherViewController alloc]initWithNibName:@"AnotherViewController" bundle:nil]; 
    [self.navigationController pushViewController:avc]; 
    } 
    //some other code if needed 
} 

希望它可以帮助

+0

得到这个错误 - 2012-05-14 13:56:45.232 SpltiView可欣赏[26907:F803] * **由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无法在软件包中加载NIB:'NSBundle Users/nihal/Library/Application Support/iPhone Simulator/5.1/Applications/9D894362-800E-4BE1-BE2B- 2F7E2C3DB8A3/SpltiView With Views.app>(已装载)'名称为'NewViewController'' ***第一次抛掷调用堆栈: 终止调用抛出异常 –

+0

并且由于主控细节已经定义了默认的导航控制器详细视图控制器 - 你认为使用[自我.navigationController pushViewController:svc]会工作吗? –

+0

@NihalSharma对不起,没有看到它是iPad – Novarg

您可以创建视图控制器的枚举为: -

enum MyViewControllers 
{ 
viewcontroller1 
viewcontroller2 
viewcontroller3 
} 

枚举的constatns会符合您的viewcontrollers每个indexPAth。

,然后在UITableView的didSelectRowAtIndexPath方法的委托方法:,使用开关的情况下: -

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath 
    { 
    switch(indexPath.row) 
{ 
    case 1: 
      Load view1 
      break; 
    case 2: 
      Load view2 
      break; 
    case 3: 
      Load view3 
      break;  . 
     . 
     so on. 
    } 
} 

或者在不同的NavigationControllers的情况下(如在iPad上),你将不得不通过使用委托和protocol.Define数据在RootViewController的,然后将协议设置其委托的详细信息(如果你想推视图 - 控制)

somethinglike: -

@protocol SendSelectedINdexDelegate 
{ 
@required 
-(void)sendTheIndex:(NSNumber)number. 
@end 

接口

id <SendSelectedINdexDelegate> delegateSend 

设置其属性: -

@property (nonatomic) id delegateSend; 

在.M

@synthesize delegateSend 

和在didSelectRowAtIndexPath方法: -

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath 
{ 
[self delegate]sendTheIndex:indexPath.row]; 

} 

在接收控制器 符合协议 并设置

rootview.delegateSend=self; 

和实现协议的方法;

-(void)sendTheIndex:(NSNumber)number 
{ 
//nsnumber is your index perform operation based on this. 
} 
+1

我不认为它会工作。请建议一些其他的东西。 –

+0

看到编辑过的一个..使用委托和协议 –

+0

[self delegate] sendTheIndex:indexPath.row]; 应为: [[self delegateSend] sendTheIndex:indexPath.row]; 但你会有一个不兼容的整数指针转换发送NSInteger到一个NSNumber参数。 –

小心,如果你正在使用ARC,它禁止与未指定所有权属性的属性合成器(在.M @synthesize delegateSend)。