2010-10-20 29 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?