CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)@"ip address", port, &readStream, &writeStream);
这只能是一个固定的IP地址,如果我想改变使用textfield输入IP。
我有两个viewcontroller
socketView
和setViewcontroller
,textfield
被放置setViewcontroller
,socketview
进程是套接字的使用。
textfield
到NSString
的价值:
NSString *ip = TxtIP.text;
我的想法是在NSString *dstIP
socketView.m
NSString *dstAddress;
-(void) initNetworkCommunication{
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)dstAddress, 8080, &readStream, &writeStream);
inputStream = ( NSInputStream *)CFBridgingRelease(readStream);
outputStream = ( NSOutputStream *) CFBridgingRelease(writeStream);
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];}
然后在setviewcontrolle.m
内设置dstIP = TxtIP.text
。
但我知道这不是直接设置的,我该如何修改呢?
答案 0 :(得分:0)
我想我找到了解决问题的方法!
我使用NSUserDefaults
来存储textfield
值:
NSString *IPaddress = [textfield text];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:IPaddress forKey:@"IPaddress"];
[defaults synchronize];
然后SocketView
阅读:
NSString *dstAddress;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *IPaddress = [defaults objectForKey:@"IPaddress"];
dstAddress=IPaddress;