Im trying to get this to work, let me explain what im trying to do and then i will show some code. Alright so i have a UITableView with a navigation controller, when you select a row, it loads a detail view.
I have made a string that will carry what row was selected, so i can know what to display on the view. The string wont pass between these two classes, and just comes up with null, even when i try to NSLog the string in another method within the same class it still comes up with null, the only place it will actually display whats inside it is in the method it was created.
Let me show you some code, i am trying to different ways to do it, and both come out with null
first class.h
@interface features : UITableViewController{
NSMutableArray *featuresTableViewSet;
NSString *selectedFeature;
}
@property (nonatomic, retain) NSMutableArray *featuresTableViewSet;
@property (nonatomic, retain) NSString *selectedFeature;
-(NSString *)get;
@end
first class.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedFeature = [[NSString alloc]init];
selectedFeature = [featuresTableViewSet objectAtIndex:indexPath.row];
// Navigation logic may go here. Create and push another view controller.
features_detail *detailViewController = [[features_detail alloc] initWithNibName:@"features_detail" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
}
-(NSString *)get{
NSLog(@"%@", selectedFeature);
return selectedFeature;
}
Now here is the class im attempting to use the string in
i #import "features.h" the first class into the second one
here is the .m for the second class
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
features *selected = [[features alloc]init];
self.title = selected.selectedFeature;
NSLog(@"%@", [selected get]);
NSLog(@"%@", selected.selectedFeature);
}
One of the two things im doing should work right?
Thanks 🙂
Oh, no, this won’t work this way. You should create NSString property in the detail view class .h file:
After you create detailViewController instance, you can initialize this property with they value you get from the table and then push the controller:
This will help to avoid memory leakage because of the unreleased object.
You don’t need the
getmethod in here, by the way.