如何使用另一个类的方法访问一个类的变量?
嗨,我的英语不是很好,所以我会尽力解释我的问题 我是 cocoa 和 xcode 的新手,所以也许我的问题比我想象的
类更容易,这基本上是注册客户信息的公式,我在那里定义了一个方法 - (void)searcht:(NSString*)s;这个方法对数据库进行查询,其中 code = s
- (void) searcht:(NSString *)s{
PGSQLConnection *connection1= [[PGSQLConnection alloc] init];
NSString *cmd=[[NSString alloc] initWithFormat:@"select * from oc_master_customer where customer_id=%@",s ];
NSLog(@"%@", cmd);
if ([connection1 connect]){
NSLog(@" ENTRO %@", [connection1 lastError]);
PGSQLRecordset *rs1 = [connection1 open:cmd];
if (![rs1 isEOF])
{
NSLog(@" ENTRO 2 %@", [connection1 lastError]);
[matchcode setStringValue:
[[rs1 fieldByName:@"matchcode"] asString]];
NSLog(@" MATCHCODE %@", [matchcode stringValue]);
NSLog(@" MATCHCODE 2 %@", [[rs1 fieldByName:@"matchcode"] asString]);
[name1 setStringValue:
[[rs1 fieldByName:@"name1"] asString]];
[zip setStringValue:
[[rs1 fieldByName:@"zip"] asString]];
[bank_code setStringValue:
[[rs1 fieldByName:@"bank_code"] asString]];
[bank_account setStringValue:
[[rs1 fieldByName:@"bank_account"] asString]];
[bank_name setStringValue:
[[rs1 fieldByName:@"bank_name"] asString]];
[bank_iban setStringValue:
[[rs1 fieldByName:@"bank_iban"] asString]];
[bank_swift setStringValue:
[[rs1 fieldByName:@"bank_swift"] asString]];
[city setStringValue:
[[rs1 fieldByName:@"city"] asString]];
[city_part setStringValue:
[[rs1 fieldByName:@"citypart"] asString]];
[street setStringValue:
[[rs1 fieldByName:@"street"] asString]];
[country setStringValue:
[[rs1 fieldByName:@"country_code"] asString]];
}
[connection1 close];
[rs1 close];}
}
我将此方法与 NSSearchfield 一起使用,它工作得很好,现在我创建了 nsearchfield 的子类,我想在用户按下 Enter 时调用此方法,我可以访问方法,但我无法更改 IBOutlet 中的字符串值。我已经将 IBOutlets 声明为 @public。我来自 nssearchfiel 子类的代码
-(void)keyUp:(NSEvent*)event
{
if ([event keyCode]==36){
[[self window] selectKeyViewFollowingView:self];
customers* c= [[customers alloc] init];
NSLog(@"Key released: %@",[self stringValue]);
[c searcht:[self stringValue]];
}
}
我的问题是,如果我从其他类调用,我定义的方法不会执行任何操作 有人知道我必须做什么或我错过了什么
Hi my english is not very good, so i'll try my best to explain my problem
I'm new with cocoa and xcode so maybe my problem is easier than i think
I have class , that's basically is a formulary for register client information, there i defined a method - (void)searcht:(NSString*)s; this method make a query to the database where the code = s
- (void) searcht:(NSString *)s{
PGSQLConnection *connection1= [[PGSQLConnection alloc] init];
NSString *cmd=[[NSString alloc] initWithFormat:@"select * from oc_master_customer where customer_id=%@",s ];
NSLog(@"%@", cmd);
if ([connection1 connect]){
NSLog(@" ENTRO %@", [connection1 lastError]);
PGSQLRecordset *rs1 = [connection1 open:cmd];
if (![rs1 isEOF])
{
NSLog(@" ENTRO 2 %@", [connection1 lastError]);
[matchcode setStringValue:
[[rs1 fieldByName:@"matchcode"] asString]];
NSLog(@" MATCHCODE %@", [matchcode stringValue]);
NSLog(@" MATCHCODE 2 %@", [[rs1 fieldByName:@"matchcode"] asString]);
[name1 setStringValue:
[[rs1 fieldByName:@"name1"] asString]];
[zip setStringValue:
[[rs1 fieldByName:@"zip"] asString]];
[bank_code setStringValue:
[[rs1 fieldByName:@"bank_code"] asString]];
[bank_account setStringValue:
[[rs1 fieldByName:@"bank_account"] asString]];
[bank_name setStringValue:
[[rs1 fieldByName:@"bank_name"] asString]];
[bank_iban setStringValue:
[[rs1 fieldByName:@"bank_iban"] asString]];
[bank_swift setStringValue:
[[rs1 fieldByName:@"bank_swift"] asString]];
[city setStringValue:
[[rs1 fieldByName:@"city"] asString]];
[city_part setStringValue:
[[rs1 fieldByName:@"citypart"] asString]];
[street setStringValue:
[[rs1 fieldByName:@"street"] asString]];
[country setStringValue:
[[rs1 fieldByName:@"country_code"] asString]];
}
[connection1 close];
[rs1 close];}
}
I use this method with a NSSearchfield and it worked perfectly, now i made a subclass of nsearchfield and i want to call this method when the user pressed enter, i could access to the method but i can't change the string values from the IBOutlets. I already have declared the IBOutlets as @public. My code from the nssearchfiel subclass
-(void)keyUp:(NSEvent*)event
{
if ([event keyCode]==36){
[[self window] selectKeyViewFollowingView:self];
customers* c= [[customers alloc] init];
NSLog(@"Key released: %@",[self stringValue]);
[c searcht:[self stringValue]];
}
}
My problem is that the method i defined doesn't do a thing if i called from other class
somebody have an idea of that what i have to do or i missing
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 IBOutlet 连接似乎已断开。当您在
-keyDown
事件中创建customers
实例时,您的所有网点均未连接。这就是它坏掉的原因。这不是继承的问题,而是面向对象设计的问题。您可能需要重新考虑您的函数如何相互通信。为了证明情况是否如此(很抱歉,但我在阅读您的代码时遇到了一些问题),请在您的
-searcht:
函数中添加一条日志语句以查看它是否存在:I'我猜你会看到类似的内容:
name1field: (null)
使用建议的解决方案更新
如果你有 IBOutlets (这让我觉得这是一个有问题的决定),这意味着它在你的笔尖中实例化。您可能想要做的(假设您没有彻底改变代码的结构)是在您的
NSSearchField
子类中创建一个插座,将其连接到您的customers
对象nib,并使用它,而不是创建customers
的新实例。不过,这是一个创可贴解决方案。您可能想阅读一些 Cocoa 代码中使用的设计模式。这可能会给您一些关于如何构建课程的更好的想法。
It looks like your IBOutlet connections are broken. When you create an instance of
customers
in the-keyDown
event, none of your outlets are connected. That's why it's broken. This is not a problem with inheritance, so much as object-oriented design. You probably need to rethink how your functions talk to each other.To prove whether this is the case (I'm sorry, but I'm having some trouble reading your code), add a log statement in your
-searcht:
function to see if it exists:I'm guessing that you'll see something like:
name1field: (null)
Updated with suggested solution
If you have IBOutlets there (which strikes me as a questionable decision), that means it's instantiated in your nib. What you will probably want to do (assuming you don't drastically alter your code's structure) is create an outlet in your
NSSearchField
subclass, connect it to thecustomers
object in your nib, and use that, instead of creating a new instance ofcustomers
.This is a band-aid solution, though. You may want to read up on some of the design patterns used in Cocoa code. That might give you some better ideas for how to structure your classes.