-1

I have a weird problem with my TableView which is always set to nil. I'll explain more:

This is my storyboard:

StoryBoard

In my BuddyListViewController.m file I instantiate my ChatViewController :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
userName = (NSString *) [onlineBuddies objectAtIndex:indexPath.row];
JCChatViewController *chatController = [[JCChatViewController alloc] initWithUser:userName];
    [self presentViewController:chatController animated:YES completion:nil];
} 

ChatViewController (.h) File

@interface JCChatViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
    UITextField *messageField;
    UITableView *tView;
}

@property (nonatomic,retain) IBOutlet UITextField *messageField;
@property (nonatomic,strong) IBOutlet UITableView *tView;

-(id) initWithUser:(NSString *) userName;
-(IBAction)sendMessage;
@end

I also linked my tableView with my tView property

ChatViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tView.delegate = self;
    self.tView.dataSource = self;
   [self.tView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
}

While debugging, I always have self.tView equal to nil, with is very bizarre, thus, my TableView Delegate methods are not invoked.

Best regards

Ali
  • 647
  • 2
  • 10
  • 28

1 Answers1

1

When you create JCChatViewController object in code, you don't create it's view. If you want it's view to be created like it was described in your storyboard you have two options:

  • Create it inside didSelectRowAtIndexPath using UIStoryboard's method

- (id)instantiateViewControllerWithIdentifier:(NSString *)identifier

You could take storyboard from your current view controller object. Also you need to specify some storyboard id to JCChatViewController in your storyboard.

  • Create a segue in storyboard, that would connect cells from your Friends List and Chat controller. You could pass any additional data to Chat controller inside method

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

that should be overridden in Friends List controller.

Artem Stepanenko
  • 3,423
  • 6
  • 29
  • 51
  • Thanks Artem, but I already tried that and same result, not working and tView = nil – Ali Jun 24 '14 at 12:13
  • 1
    @Ali, which approach did you try? – Artem Stepanenko Jun 24 '14 at 12:15
  • I tried the Segue approach, yesterday, and that did not work, and, I was trying the instantiateViewControllerWithIdentifier, and every time my tView is nil ?! – Ali Jun 24 '14 at 12:22
  • 1
    @Ali, the code that you've posted here definitely doesn't connect view from storyboard to Chat view controller. So you should try to find out the reason why mentioned options didn't work for you. – Artem Stepanenko Jun 24 '14 at 12:27
  • Thank you I re-wrote my prepareForSegue Method, and the results are much better, I can see my TableView on the screen, without Data but that's not a problem. Thanks again and have a nice Day/evening/night – Ali Jun 24 '14 at 12:40