2010-10-20 113 views
0

HI 我正在开发基于UITablview的项目。当单元格点击时,我喜欢加载新视图。我在didselectRowAtIndexPath委托方法中使用以下代码。 下面的代码没有显示任何错误,但新视图没有得到加载,[self navigationController]返回null。在tableview委托中返回null

里面Viewdidload函数[self navigationcontroller]返回适当的值。但是在Delegate方法[self navigationcontroller]中返回null。

/// inside appDelegate class 
     - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

// Override point for customization after application launch. 

self.viewController = [[LAMainViewController_iPhone alloc]initWithNibName:@"LAMainViewController_iPhone" bundle:nil]; 

UINavigationController *controller = [[UINavigationController alloc]initWithRootViewController:viewController]; 

[window addSubview:controller.view]; 

[controller release]; 

[window makeKeyAndVisible]; 

return YES; 
} 




    /// inside LAmainviewcontroller.m 


    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 

if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { 

    // Custom initialization 
    } 

//self.navigationController.navigationBar.backgroundColor =[UIColor clearColor];// [email protected]"test"; 

return self; 
    } 



     - (void)viewDidLoad { 

    [super viewDidLoad]; 

    NSString *materialPlistPath = [[NSBundle mainBundle]pathForResource:@"materials" ofType:@"plist"]; 

materialList = [[NSArray alloc]initWithContentsOfFile:materialPlistPath]; 

materialTable.backgroundColor = [UIColor blackColor]; 

NSLog(@" dud kiad navigationController %@", self.navigationController); 

//2010-10-20 15:22:03.809 LabAssistant[17368:207] dud kiad navigationController <UINavigationController: 0x5f3b160> 

    } 

    -(void)viewWillAppear:(BOOL)animated{ 

[super viewWillAppear:YES]; 

self.navigationController.navigationBarHidden = YES; 

NSIndexPath *indexPath = [materialTable indexPathForSelectedRow]; 

[materialTable deselectRowAtIndexPath:indexPath animated:YES]; 

    } 





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

    LAMaterialPropertiesViewController_iPhone *materialPropertyListView = [[[LAMaterialPropertiesViewController_iPhone alloc] initWithNibName:@"LAMaterialPropertiesViewController_iPhone" bundle:nil] autorelease]; 

    materialPropertyListView.chemicalName = [[materialList objectAtIndex:[indexPath row]] objectForKey:@"materialProperty"]; 

    [[self navigationController] pushViewController:materialPropertyListView animated:YES]; 
    NSLog(@"%@",[self navigationController]); 

///2010-10-20 16:20:42.634 LabAssistant[17656:207] navigationController (null) 
} 

请帮我解决这个问题。 谢谢!

+0

没有足够的信息来帮助你。代码看起来很好。是否有错误讯息?如果不是,则发布所有代码。 – Jordan 2010-10-20 10:59:29

+0

@Jordan我已经用真正的编码更新了我的问题,你现在可以检查它 – Ram 2010-10-20 11:10:57

回答

0

从init语句中删除autorelease。实际上,viewcontroller在被推送之前就已经发布了。

代替

LAMaterialPropertiesViewController_iPhone *materialPropertyListView = [[[LAMaterialPropertiesViewController_iPhone alloc] initWithNibName:@"LAMaterialPropertiesViewController_iPhone" bundle:nil] autorelease]; 

试试这个

LAMaterialPropertiesViewController_iPhone *materialPropertyListView = [[LAMaterialPropertiesViewController_iPhone alloc] initWithNibName:@"LAMaterialPropertiesViewController_iPhone" bundle:nil]; 

希望这将解决您的问题。

+0

@Atulkumar V. Jain。我已经尝试过,但它仍然没有工作[自我导航控制器]返回null。 – Ram 2010-10-20 11:11:43

+0

这可能是bcoz导航栏被隐藏。将navigationbar.hidden设置为NO,然后让我知道。 – 2010-10-20 11:26:23

+2

使用UINavigationController * controller = [[[UINavigationController alloc] initWithRootViewController:viewController] retain]; – Gyani 2010-10-20 11:38:46

0

我认为在导航控制器被设置为视图控制器之前调用表视图委托。

你可以尝试重新加载tableview [tableview reloadData];在viewWillAppear或viewDidAppear?