How to set a NSString in a class from a uitextfield from a viewcontroller
I am tying to set NSString *receiveCodeText; thats in an object with a uitextfiled value thats in my viewcontroller from my v开发者_JAVA百科iewcontroller inside tableView:didSelectRowAtIndexPath: however I am getting an error
/Users/imac/Documents/Iphone applications/tables/Classes/RootViewController.m:198:0 /Users/imac/Documents/Iphone applications/tables/Classes/RootViewController.m:198: error: accessing unknown 'setReceiveCodeText:' class method
/Users/imac/Documents/Iphone applications/tables/Classes/RootViewController.m:198:0 /Users/imac/Documents/Iphone applications/tables/Classes/RootViewController.m:198: error: object cannot be set - either readonly property or no setter found
here is my code and how I am trying to pass the text.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
searchTableViewController *searchTable = [[searchTableViewController alloc] initWithNibName:@"searchTableViewController" bundle:nil];
switch (indexPath.row) {
case 1: {
searchTable.editedFieldName = @"Make";
//Pass code number over to DBAccess class
DBAccess.receiveCodeText = self.codeText.text;
} break;
case 2: {
searchTable.editedFieldName = @"Model";
} break;
case 3: {
searchTable.editedFieldName = @"Year";
} break;
}
[self.navigationController pushViewController:searchTable animated:YES];
[searchTable release];
}
DBAccess
is a class. Unless setRecieveCodeText:
is a class method, you can't use it directly. Properties belong to the instance rather than the class so you will have to declare class methods and use a static variable to handle this. However it makes sense to instantiate and then use that object or even using a singleton if you want to avoid multiple instances of the same class. A singleton should be available for use across classes.
I've included code related to the class method approach.
@interface DBAccess: NSObject {
}
+ (NSString *)receiveCodeText;
+ (void)setReceiveCodeText:(NSString *)code;
[..]
@end
In the .m
file,
static NSString * receiveCodeText;
@implementation DBAccess
[..]
+ (NSString *)receiveCodeText {
return receiveCodeText;
}
+ (void)setReceiveCodeText:(NSString *)code {
[receiveCodeText autorelease];
receiveCodeText = [code copy];
}
[..]
@end
精彩评论