Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 来自服务器的消息不';不会显示在文本视图中,但可以与NSlog配合使用_Objective C_Sockets_Textview - Fatal编程技术网

Objective c 来自服务器的消息不';不会显示在文本视图中,但可以与NSlog配合使用

Objective c 来自服务器的消息不';不会显示在文本视图中,但可以与NSlog配合使用,objective-c,sockets,textview,Objective C,Sockets,Textview,我现在就要吃我的键盘了。 我编写了一个程序来发送和接收来自服务器的消息。发送消息没有问题。但当我尝试使用文本视图显示所有消息时,只有我发送的消息才会显示 - (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode{ NSLog(@"stream event %i", eventCode); switch (eventCode) { case NSStreamEventOpenCompleted:

我现在就要吃我的键盘了。 我编写了一个程序来发送和接收来自服务器的消息。发送消息没有问题。但当我尝试使用文本视图显示所有消息时,只有我发送的消息才会显示

- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode{
NSLog(@"stream event %i", eventCode);
switch (eventCode) {
    case NSStreamEventOpenCompleted:
        NSLog(@"Stream opened");
        break;
    case NSStreamEventHasBytesAvailable:
        if (aStream == inPutStream) {
            uint8_t buffer[1024];
            int len;
            while ([inPutStream hasBytesAvailable]) {
                len = [inPutStream read:buffer maxLength:sizeof(buffer)];
                if (len > 0) {
                    Message = [[NSString alloc] initWithBytes:buffer
                                                       length:len encoding:NSASCIIStringEncoding];
                    [self recvMessage:Message];
                }
            }
        }
        break;
    default:
        ;
}
}

- (void)recvMessage:(NSString *)output{
self.textView.text = [NSString stringWithFormat:@"%@\n%@",self.textView.text,output];
}
这是标题

@interface ViewController : UIViewController <NSStreamDelegate>
- (IBAction)backGround2:(id)sender;
- (IBAction)backGround:(id)sender;
@property (retain, nonatomic) IBOutlet UITextView *textView;
@property (retain, nonatomic) IBOutlet UITextField *textFeild;
- (IBAction)sendBotton:(id)sender;
@property (retain, nonatomic) IBOutlet UITextField *hostName;
@property (retain, nonatomic) IBOutlet UIView *MainView;
@property (retain, nonatomic) IBOutlet UIView *chatView;
- (IBAction)connectBotton:(id)sender;
- (IBAction)disConnect:(id)sender;

@end

NSString *Message;
NSInputStream *inPutStream;
NSOutputStream *outPutStream;    
我可以在目标输出中看到消息

服务器上运行的客户端肯定已发送消息。

请重试

- (void)recvMessage:(NSString *)output
{
        if ([txtvw.text length] > 0)
        {
            txtvw.text = [NSString stringWithFormat:@"%@\n%@", txtvw.text, output];
        }
        else
        {
            txtvw.text = [NSString stringWithFormat:@"%@", output];
        }
}

听起来像是一个线程问题,网络发生在后台线程上,因此您没有在UI(即主)线程上更新

你可以试试这个快速修复。替换:

[self recvMessage:Message];


或者等效的gcd变体。

检查Xib中是否提供了TextView插座。是的,它在那里,而且总是在那里,永远不会跑掉lol谢谢。当它在运行循环中时,我似乎无法对ui进行任何更改。我很困惑尝试在RecVMMessage函数中打印输出工作正常,我看到服务器的回复尝试我更新的ans,如果不起作用,则只需将一些演示文本设置为textview,如textview.text=@“test”;,如果它能工作,那么我的ans肯定能为你工作。我使用soryboard,里面有两个ViewController。这有意义吗?不,没有意义,只需在代码上方注释并设置一些演示文本,如textView.text=@“test”;表示您的插座设置不正确。但我有另一个按钮,可以在textView中更改字符串,效果很好。
[self recvMessage:Message];
[self performSelectorOnMainThread:@selector(recvMessage:) withObject:Message waitUntilDone:NO];