在tableview委托中返回null

时间:2010-10-20 10:19:49

标签: iphone objective-c uitableview

HI 我正在研究基于UITablview的项目。我喜欢在单元格点击时加载新视图。我在didselectRowAtIndexPath委托方法中使用以下代码。 下面的编码没有显示任何错误,但新视图没有加载,[self navigationController]返回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];// .title=@"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)
 }

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

2 个答案:

答案 0 :(得分: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];

希望这能解决您的问题。

答案 1 :(得分:0)

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

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