Search code examples
uitableviewuiviewcontrollerxibnavigationcontrollerawakefromnib

how to load xib view(UIViewController) from xib(UITableViewCell)


Here is what I already have;

  • I have loaded a Custom cell from XIB file (UITableViewCell)
  • This custom cell contains Button A and B
  • Created a IBAction to handle Button A
  • loaded above XIB file in MyTableView class (UIViewController)

Now I want to load another view(Most probably a XIB file) on Button A clicked event, in IBAction function I have tried this;

    // Where MyTableView is a class which loads this custom cell
    // ModalView is a name of XIB File
    MyTableView *vc = [[MyTableView alloc] initWithNibName:@"ModalView" bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];

but it throughs error at "self.navigationController", says Property navigationController not found on object of type MyTableViewCell class, i know this is happening due to the UITableViewCell class and the self in this line is referring to the UIViewController Object and I should use awakeFromNib or something like that, but I couldn't find any sufficient example to let me achieve what I need.


Solution

  • ok, I have solved my problem;

    In cellForRowAtIndexPath function, where I was fetching the XIB file (UITableViewCell) to represent a custom designed cell, I created a button and assign it the button from the XIB view and then attached the IBAction to it;

        // SimpleTableCell is the XIB filename (UITableViewCell)
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
    
    
        UIButton *btn = [[UIButton alloc] init];
        btn = cell.btn;
        [btn addTarget:self action:@selector(smsBtnClicked:) 
                                forControlEvents:UIControlEventTouchUpInside];