开发者

Problem with custom class in Objective-C

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 {
     TableViewContr开发者_C百科oller *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,


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);
}


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜