2012-05-14 26 views
0

我有一個主 - 細節應用程序,我想加載一個新的視圖,在主點擊行。如果我選擇第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://stackoverflow.com/questions/9272244/uisplitviewcontroller-on-ipad-with-storyboards – 2012-05-15 02:47:37

回答

0

有一個偉大的方法:

- (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(已裝載)'名稱爲'NewViewController'' ***第一次拋擲調用堆棧: 終止調用拋出異常 –

+0

並且由於主控細節已經定義了默認的導航控制器詳細視圖控制器 - 你認爲使用[自我.navigationController pushViewController:svc]會工作嗎? –

+0

@NihalSharma對不起,沒有看到它是iPad – Novarg

0

您可以創建視圖控制器的枚舉爲: -

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參數。 –

0

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

相關問題