2012-10-29 183 views
0

所以我有幾個視圖控制器在IB中設置。一個具有大約6個單元格的TableView(以編程方式填充)。然後再添加6個視圖控制器(每個單元格一個)。以編程方式切換視圖

我想要讓這個在我

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

} 

我可以使視圖切換到哪個觀點是相對於單擊單元格。

所以,如果他們點擊cell1,它會去view1,如果他們點擊cell2,它會去view2。

這是我如何初始化我的表只是順便說一句,它工作正常。

//tableview datasource delegate methods 
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
    return 1; 
} 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return showArray.count; 
} 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

static NSString *CellIdentifier = @"Cell"; 

CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 


if(cell == nil){ 
    cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 

cell.showLabel.text = [showArray objectAtIndex:indexPath.row]; 
cell.image.image = [UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]]; 
cell.seasonLabel.text = [seasonArray objectAtIndex:indexPath.row]; 

return cell; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
return 106; 
} 
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation: (UITableViewRowAnimation)animation{ 


NSIndexPath *newCellPath = [NSIndexPath indexPathForRow:showArray.count 
               inSection:0]; 

[self.theatreView insertRowsAtIndexPaths:@[newCellPath] 
         withRowAnimation:UITableViewRowAnimationFade]; 

} 

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

} 
+0

你怎麼讓你的應用程序結構化。它坐在導航控制器中嗎? – Bergasms

+0

@Bergasms不,它有一個視圖控制器是主菜單,它只是從控制器到控制器使用UIButtons – tyler53

回答

0

根據您的應用程序的結構,我會建議這兩種方法之一。

[self.navigationController pushViewController:newViewController animated:YES]; 


[self presentModalViewController:newViewController animated:YES]; 

//for ios 6.0 
[self presentViewController:newViewController animated:animated completion:NULL]; 

從我可以收集的信息來看,在主菜單的.xib文件中創建了很多uiviewcontrollers。那是對的嗎?所以你會有self.theatreViewController。然後您可以將其作爲按鈕回調中的參數傳遞。例如

[self presentViewController:self.theatreViewController animated:animated completion:NULL]; 

糾正我,如果我錯了我的假設在這裏。

+0

presentModalViewController:animated:在iOS 6中不贊成使用。並且還獲取警告不兼容指針類型發送'TheatreViewController * _strong'到UIViewController類型的參數我寫的是[self presentModalViewController:TheatreViewController animated:YES]; – tyler53

+0

您需要將實際變量傳遞給presentModalViewController。例如TheatreViewController * myviewController,你可以通過myviewController進入。另外,我也忘記了ios6的棄用。相反,你可以使用[self presentViewController:modalViewController animated:animated completion:NULL]; – Bergasms

+0

這將工作(您編輯您的答案),但theatreViewController不是Viewcontroller的屬性,所以我不能使用self.theatreViewController但是,你是對的我有很多視圖控制器,我只是想在它們之間切換。編輯:現在單擊按鈕時出現此錯誤。 'NSInvalidArgumentException',原因:'應用程序試圖在目標上呈現一個無模式視圖控制器。 – tyler53

0

我不知道你的應用程序的結構,但我想你可能會考慮這個問題:

- (UIViewController *)getViewControllerAtIndexPath:(NSIndexPath *)indexPath { 
    //..you can use "switch" to return the view controller you want 
    return ...; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UIViewController *controller = [self getViewControllerAtIndexPath:indexPath]; 
    //..push or present your controller 
} 
0

使用指數路徑財產此方法: (無效)的tableView:(UITableView的* )的tableView didSelectRowAtIndexPath方法:(NSIndexPath *)indexPath

yourObject = [yourArray objectAtIndex:indexPath];

編輯: - 在下面的方法AppDelegate中,做到這一點: - 現在

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

{ 

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 
// Override point for customization after application launch. 

    mHomeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil]; 
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:mHomeViewController]; 
    [mHomeViewController release]; 

    self.window.rootViewController = navController; 
    self.window.backgroundColor = [UIColor whiteColor]; 
    [self.window makeKeyAndVisible]; 
    [navController release]; 
    return YES; 
} 

,在你的viewController - didSelectRowMethod,這樣做:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString * object = [yourArray objectAtIndex:indexPath.row]; 

    [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; 

    if(newControllerInstance == nil) // check if it's nil, then initialize and alloc it. 
    { 
     newControllerInstance = [[NewViewController alloc] initWithNibName:@"NewViewController" bundle:nil]; 
     [self.navigationController newControllerInstance animated:YES]; 
     [newControllerInstance release]; 
    } 
} 

這應該工作。

+0

我知道如何找到索引,如果您閱讀下面的評論,似乎我的視圖控制器由於某種原因是零。我不知道爲什麼 – tyler53

+0

如果你只是想推視圖,使用[self.navigationController pushViewController:ViewController animated:YES]; ,你是否初始化並分配你的視圖控制器? –

+0

我沒有導航控制器,通過初始化和分配它,你是什麼意思。你的意思是我有 - (id)初始化和東西? – tyler53

相關問題