I’m learning Cocoa…
I tried different way to do that but I’m still in the black…
I have this method in my implementation:
- (void)closeStream:(NSStream *)theStream {
[theStream close];
[theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
How can I call it from an IBAction in my @synthetize?
- (IBAction)connect:(id)sender {
if ([[connectNOK stringValue] isEqualToString:@"Disconnected"]) {
[connectButton setTitle:@"Disconnect"];
NSString * hostFromField = [hostField stringValue];
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)hostFromField, [portField intValue], &readStream, &writeStream);
inputStream = (NSInputStream *)readStream;
outputStream = (NSOutputStream *)writeStream;
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];
} else {
[connectButton setTitle:@"Connect"];
// I want to call this method here
}
}
If the
closeStream:method is defined in the same class than theconnect:method, you’ll have to use:Where
someStreamis theNSStreamobject you need to pass.The
selfkeyword refers to the current instance of the class.If you don’t know that, or what it means, I suggest you take a look at the Objective-C language basics before trying to do/code anything, and maybe after, the complete language reference.
EDIT:
I can see in your code that your
connect:method «toggles» the connection based on the value of a button label.This is not a very good design, for you to know, but you’ll have other problems here.
I guess you want to close the input and output streams, if necessary.
The problem is, when the
connectmethod is called a second time, theinputStreamandoutputStreamvariables are not accessible anymore, as they are local variables.You probably need to store them as instance variables instead, so you can refer to them later on.
Once again, it seems you really should start by reading some documentation about programming principles, as well as some Object-Oriented programming principles.
Don’t try to go too fast. Knowledge is the key to everything, so start by reading the documentation I mentioned before.