0

I have a little problem with the application I currenty work on. I create a simpliest project to illustrate my problem.

So, I create a "Navigate-Base Application". I add an other UITableViewController named TableViewController (the one which is created with the project is named RootViewController). I create an instance of TableViewController when I touch a line in the RootViewController.

I create a custom class named "MyCustomClass".

MyCustomClass.h (full code) :

#import <Foundation/Foundation.h>

@interface MyCustomClass : NSObject {
    NSString *name;
}

@property (nonatomic, retain) NSString * name;

@end

MyCustomClass.m (full code) :

#import "MyCustomClass.h"

@implementation MyCustomClass

@dynamic name;

@end

I had a MyCustomClass attibute in TableViewController class.

TableViewController.h (full code) :

#import <UIKit/UIKit.h>
#import "MyCustomClass.h"

@interface TableViewController : UITableViewController {
    MyCustomClass *aCustomObject;
}

@property (nonatomic, retain) MyCustomClass *aCustomObject;

@end

At the load of TableViewController, I try to display aCustomObject's content.

TableViewController.m (top of the file and what I modify in the template's file) :

#import "TableViewController.h"

@implementation TableViewController

@synthesize aCustomObject;

#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    NSLog(@"Name : %@",self.aCustomObject.name);
}

Before, I create and give a value to aCustomObject.name in RootViewController :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     TableViewController *detailViewController = [[TableViewController alloc] initWithNibName:@"TableViewController" bundle:nil];
    detailViewController.aCustomObject.name = @"The Name";
    [self.navigationController pushViewController:detailViewController animated:YES];
}

Console said :

2011-06-22 07:21:11.087 MyTestApp[12822:207] Name : (null)

I think it's a stupid thing but I don't find myself after hours of try.

Thanks a lot and excuse me for my english mistakes,

2 Answers 2

2

You forget to initialize your custom object in the tableViewController's viewDidLoad Method.

Try this.

- (void)viewDidLoad {
[super viewDidLoad];
if(aCustomObject == nil){
   self.aCustomObject = [[[MyCustomClass alloc] init] autoRelease];
 }
self.aCustomObject.name = @"";
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
 //this will show empty here.
NSLog(@"Name : %@",self.aCustomObject.name);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks but it doesn't work. It crash with the message "[MyCustomClass setName:]: unrecognized selector sent to instance 0x4e12c40". And if I initialize my object in ViewDidLoad, I can't give a value when I create TableViewController in didSelectRowAtIndexPath's method of RootViewController.
2

You use the @dynamic keyword to tell the compiler that you will fulfill the API contract implied by a property either by providing method implementations directly or at runtime using other mechanisms such as dynamic loading of code or dynamic method resolution. It suppresses the warnings that the compiler would otherwise generate if it can’t find suitable implementations. You should use it only if you know that the methods will be available at runtime.

from Apple Documentation

You are claiming in the question that you included full source for MyCustomClass.m. Where did you implement the getter and setter for the property? If you want the compiler to generate the methods for you, you should use

@synthesize name;

1 Comment

Yes, you are perfectly right ! A f*#;+# copy/past from an class which inherits from NSManagedObject makes me become almost crazy. And after, I initialize aCustomObject and It works fine. Thank you very much both of you !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.