如何在视图控制器的uitextfield中设置类中的NSString

时间:2011-06-09 03:08:42

标签: iphone ios uitableview uitextfield

我想要设置NSString * receiveCodeText;那个带有uitextfiled值的对象,在我的viewcontroller中,来自我在viewView中的viewcontroller:didSelectRowAtIndexPath:但是我收到了一个错误

  

/用户/的iMac /文档/ iPhone   applications / tables / Classes / RootViewController.m:198:0 / Users / imac / Documents / Iphone   应用程序/表/班/ RootViewController.m:198:   错误:访问未知   'setReceiveCodeText:'类方法

     

/用户/的iMac /文档/ iPhone   applications / tables / Classes / RootViewController.m:198:0 / Users / imac / Documents / Iphone   应用程序/表/班/ RootViewController.m:198:   错误:无法设置对象 - 或者   readonly property或no setter found

这是我的代码以及我如何传递文本。

- (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];
}

1 个答案:

答案 0 :(得分:0)

DBAccess是一个班级。除非setRecieveCodeText:是类方法,否则不能直接使用它。属性属于实例而不是类,因此您必须声明类方法并使用静态变量来处理它。但是,如果要避免同一个类的多个实例,则实例化然后使用该对象甚至使用单例是有意义的。单例应该可用于跨类。

我已经包含了与类方法相关的代码。

@interface DBAccess: NSObject {
}

+ (NSString *)receiveCodeText;
+ (void)setReceiveCodeText:(NSString *)code;
[..]
@end

.m文件中,

static NSString * receiveCodeText;

@implementation DBAccess
[..]
+ (NSString *)receiveCodeText {
    return receiveCodeText;
}

+ (void)setReceiveCodeText:(NSString *)code {
    [receiveCodeText autorelease];
    receiveCodeText = [code copy];
}
[..]
@end