Sahil Khanna
Sahil Khanna

Reputation: 4382

Inheritance in UIViewController

I'm working over inheriting UIViewControllers but am facing issues with it. Heres the list of ViewControllers I've used and their flow.

Header Files

MainViewController : UIViewController {

}

CustomerTicketViewController : UIViewController {

}
@property (nonatomic, retain) NSArray *listTickets;

CustomerEditTicketViewController : CustomerTicketViewController {

}

Implementation Files

@implementation MainViewController

- (void)loadCustomer {
        CustomerTicketViewController *customerTicketViewController = [[CustomerTicketViewController alloc] initWithNibName:@"CustomerTicketViewController" bundle:nil];
        [customerTicketViewController setListTickets:myTickets];
        [self presentModalViewController:customerTicketViewController animated:YES];
        [customerTicketViewController release];
}

@end

@implementation CustomerTicketViewController

    - (void)editCustomer {
        CustomerEditTicketViewController *customerEditTicketViewController = [[CustomerEditTicketViewController alloc] initWithNibName:@"CustomerEditTicketViewController" bundle:nil];
        NSLog(@"ParentView.listTickets: %@", listTickets);
        [self presentModalViewController:customerEditTicketViewController animated:NO];
        [customerEditTicketViewController release];
    }

    @end

@implementation CustomerEditTicketViewController

- (void)viewDidLoad {
    NSLog(@"listTickets: %@", listTickets);
    NSLog(@"super.listTickets: %@", super.listTickets);
    NSLog(@"self->listTickets: %@", self->listTickets);
    NSLog(@"self.listTickets: %@", self.listTickets);
}

@end

The logs in the sub class print null but as per my understanding they should print the same values as in the ParentView. Please guide me if I'm wrong at some place.

    ParentView.listTickets: (
    "<CustomerTicket: 0x4c75d90>",
    "<CustomerTicket: 0x4c76310>"
)

listTickets: (null)
super.listTickets: (null)
self->listTickets: (null)
self.listTickets: (null)

Upvotes: 0

Views: 1313

Answers (2)

John Haager
John Haager

Reputation: 2115

Your view controller's editCustomer method never makes an assignment to the property of the child controller.

Upvotes: 0

jrturton
jrturton

Reputation: 119242

Your edit view controller is a separate object. All it inherits from the superclass is the fact that it has an array property called listTickets,, not the value of the property. This is a (the?) fundamental point in object oriented programming.

You have to set the value after creating the view controller just as you do when creating the first one:

CustomerEditTicketViewController *customerEditTicketViewController = [[CustomerEditTicketViewController alloc] initWithNibName:@"CustomerEditTicketViewController" bundle:nil];

customerEditTicketViewController.listTickets = self.listTickets;

Upvotes: 3

Related Questions