Reputation: 1459
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 :)
Upvotes: 0
Views: 578
Reputation: 1
make nsstring object in appdelegate class and allocate this in appdelegate and declare property.and you can use of that string in whole project where you want to use that string there first you make object of appdelegate class after that you can use.
Upvotes: 0
Reputation: 2551
Oh, no, this won't work this way. You should create NSString property in the detail view class .h file:
@interface features_detail : <bla-blaController> {
NSString *selectedFeature;
}
@property (nonatomic, retain) NSString *selectedFeature;
After you create detailViewController instance, you can initialize this property with they value you get from the table and then push the controller:
features_detail *detailViewController = [[features_detail alloc] initWithNibName:@"features_detail" bundle:nil];
//pass it like this:
detailViewController.selectedString = selectedFeature;
[self.navigationController pushViewController:detailViewController animated:YES];
// Now don't forget to release the controller you've pushed:
[detailViewController release];
//Oh, and you need to release the string as well:
[selectedFeature release];
This will help to avoid memory leakage because of the unreleased object.
You don't need the get
method in here, by the way.
Upvotes: 2
Reputation: 2310
In the viewDidLoad() method, features_detail class is creating a brand new instance of the features class. The brand new instance will have its selectedFeature field initial; it won't contain the value that you set when the table row was clicked.
Here is an alternative, let the features_detail class have a field called selectedFeature. After creating an instance of the features_detail class in the didSelectRowAtIndexPath method, pass the selectedFeature to this instance.
It will look something like:
detailViewController.selectedFeature = selectedFeature;
That way, you pass what is selected from the features class to the features_detail class. (Also, there are some memory management problems in your code that we can tackle after solving the main issue)
Upvotes: 1