2013-08-19 62 views
2

我想讓我的第一個應用程序。我有6個文件:從UItableViewCell調用視圖

LeftPanelViewController.h LeftPanelViewController.m LeftPanelViewController.xib ForecastViewController.h ForecastViewController.m ForecastViewController.xib

裏面LeftPanelViewController.xib我有一個包含一個表視圖的視圖。我想從LeftPanelViewController.xib中的表視圖中設置一個單元格,當我按下該單元格時,我將能夠調用ForecastViewController.xib中的視圖。

我已在LeftPanelTableViewController.m

#import "LeftPanelViewController.h" 
#import "ForecastViewController.h" 


@interface LeftPanelViewController() 

@property (nonatomic, weak) IBOutlet UITableView *myTableView; 
@property (nonatomic, weak) IBOutlet UITableViewCell *cellMain; 
@property (copy, nonatomic) NSArray *MRP; 

@end 

@implementation LeftPanelViewController 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

Array = [[ NSMutableArray alloc] init]; 
[Array addObject:@"Forecast"]; 
[Array addObject:@"Inventory"]; 

} 

- (NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section 
{ 
return [Array count]; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView 
cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: 
SimpleTableIdentifier]; 
if (cell == nil) { 
cell = [[UITableViewCell alloc] 
initWithStyle:UITableViewCellStyleSubtitle 
reuseIdentifier:SimpleTableIdentifier]; 

//add an image to the tablecell 
UIImage *image = [UIImage imageNamed:@"Icon.png"]; 
cell.imageView.image = image; 

} 
cell.textLabel.text = [Array objectAtIndex:indexPath.row]; 

if (indexPath.row < 7) { 
cell.detailTextLabel.text = @"Account"; 
} else { 
cell.detailTextLabel.text = @"Settings"; 
} 
return cell; 
} 

- (void)viewDidUnload 
{ 
[super viewDidUnload]; 
} 

#pragma mark - 
#pragma mark View Will/Did Appear 

- (void)viewWillAppear:(BOOL)animated 
{ 
[super viewWillAppear:animated]; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
[super viewDidAppear:animated]; 
} 

#pragma mark - 
#pragma mark View Will/Did Disappear 

- (void)viewWillDisappear:(BOOL)animated 
{ 
[super viewWillDisappear:animated]; 
} 

- (void)viewDidDisappear:(BOOL)animated 
{ 
[super viewDidDisappear:animated]; 
} 

#pragma mark - 
#pragma mark Array Setup 


#pragma mark - 
#pragma mark UITableView Datasource/Delegate 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
return 2; 
} 


#pragma mark - 
#pragma mark Default System Code 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
if (self) 
{ 
} 
return self; 
} 

- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 

} 

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

if ([[Array objectAtIndex:indexPath.row] isEqual:@"Forecast"]) { 

ForecastViewController *Forecast = [[ ForecastViewController alloc] initWithNibName:@"ForecastViewController" bundle:nil]; 
[self.navigationController pushViewController:Forecast animated:YES]; 

} 


[tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 



@end 
+0

什麼問題? –

+0

當我按下指定的單元格時,什麼也沒有發生。 Xcode不會在我的代碼中顯示任何警告或錯誤,我認爲我錯過了一些東西,但由於缺乏經驗,我不能說出什麼。 – Fxbaez

+0

我在LeftPanelViewController.xib的tableview中有一個叫做forecast的單元格。我認爲這段代碼,我在LeftPanelViewController.m將允許我選擇該單元格,並將我帶到ForecastViewControler.xib中的視圖if([[Array objectAtIndex:indexPath.row] isEqual:@「Forecast」 ]){ ForecastViewController * Forecast = [[ForecastViewController alloc] initWithNibName:@「ForecastViewController」bundle:nil]; [self.navigationController pushViewController:Forecast animated:YES]; } – Fxbaez

回答

1

而不是

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

使用下面的代碼

[self presentViewController:Forecast animated:YES completion:nil]; 
+0

非常感謝你!剛剛解決了我的問題!我花了4天的時間試圖找出問題所在,並且做到了! – Fxbaez

+0

雖然這工作,他們不是一回事。第一個嘗試將視圖呈現爲導航控制器內部的推送;第二種是模式化的。有關更多信息,請參閱以下內容:http://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html –

1

即使模態控制器的伎倆,也可能是不你想要什麼。什麼都沒有發生,因爲你的視圖控制器是零。可能您正在使用xCode中的「單視圖應用程序」模板,並且它沒有導航控制器。如果你想要一個,你應該像[[UINavigationController alloc] initWithRootViewController:]創建它,並將你的窗口根控制器設置爲新創建的navigationcontroller。