Hi all,
i'm going to be a bit confused trying to initialize a class within another class.
My test app has inside:
classes (each one has .h and .m file).
RootViewController has inside a Table; my scope is add one item into the table, then click it and see in a different view some details. So i created the following code:
Test.h
TestViewController.h
RootViewController.h
RootViewController.m
This is what happen now:
- i'm able to add one item into the grid
- i'm able to push the item and open the second view
This is what i want:
- add one item into the grid (ok)
- initialize TestViewController and initialize automatically also Test in the same class (TestViewController) (KO)
- touch the item (ok)
- visualize TestViewController view popolate as the item added by code inside the grid, the same i touched before. (KO)
I try to draw the structure
[RootViewController] -->initializes--> [TestViewController] --here i want that TestViewController initializes [Test]
Now when i try to initialize Test inside TestViewController the app dies.
How can i initialize a class and initialize a second one within the firts? I hope my request is clear
Ciao,
stè
i'm going to be a bit confused trying to initialize a class within another class.
My test app has inside:
- Test
- TestViewController
- RootViewController
classes (each one has .h and .m file).
RootViewController has inside a Table; my scope is add one item into the table, then click it and see in a different view some details. So i created the following code:
Test.h
Code:
#import <Foundation/Foundation.h>
@interface Test : NSObject {
NSString *name;
NSString *description;
}
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * description;
- (id) initWithName: (NSString *) newName
description: (NSString *) newDescription;
@end
TestViewController.h
Code:
#import <UIKit/UIKit.h>
@class Test;
@interface TestViewController : UIViewController <UITextFieldDelegate> {
UITextField *name;
UITextField *description;
Test *test;
}
@property (nonatomic, retain) IBOutlet UITextField *name;
@property (nonatomic, retain) IBOutlet UITextField *description;
@property (nonatomic, retain) Test *test;
@end
RootViewController.h
Code:
@class Test;
@class TestViewController;
@interface RootViewController : UITableViewController {
NSMutableArray *listTests;
TestViewController *testController;
}
@property (nonatomic, retain) TestViewController *testController;
@end
RootViewController.m
Code:
#import "RootViewController.h"
#import "Test.h";
#import "TestViewController.h";
@implementation RootViewController
@synthesize eventController;
- (void)viewDidLoad {
[super viewDidLoad];
listTests = [[NSMutableArray alloc] init];
Test *test = [[Test alloc] initWithName:@"Primo test" description:@"Questo è il primo test inserito"];
[listTests addObject:test];
[test release];
TestViewController *newTestController = [[TestViewController alloc] init];
self.testController = newTestController;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
}
[cut...]
// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Test *selectedTest = [listTests objectAtIndex:indexPath.row];
self.TestController.test = selectedTest;
[self.navigationController pushViewController:TestController animated:YES];
}
This is what happen now:
- i'm able to add one item into the grid
- i'm able to push the item and open the second view
This is what i want:
- add one item into the grid (ok)
- initialize TestViewController and initialize automatically also Test in the same class (TestViewController) (KO)
- touch the item (ok)
- visualize TestViewController view popolate as the item added by code inside the grid, the same i touched before. (KO)
I try to draw the structure
[RootViewController] -->initializes--> [TestViewController] --here i want that TestViewController initializes [Test]
Now when i try to initialize Test inside TestViewController the app dies.
How can i initialize a class and initialize a second one within the firts? I hope my request is clear
Ciao,
stè