求助!
storyboard 中 布置了两个viewcontroller界面,暂且分别叫A界面和B界面,它们之间使用 storyboard segue 链接方式(shou push),其中A界面为首界面。
实现功能是:当接收到网络命令时,刷新A界面里面的按钮排布、显示或者隐藏。
问题是:刚打开app时,仅在首页时,网络命令接收到后都能自动更新A界面的布局,一旦点击进入到B界面后再回到A界面,之后收到的网络命令,A界面不再能自动更新排布了!?以上,求解,望大神赐教新手!

解决方案 »

  1.   

    //
    //  ViewController.m
    //  test
    //
    //  Created by z on 2017/3/28.
    //  Copyright © 2017年 test. All rights reserved.
    //#import "ViewController.h"
    #import "GCDAsyncUdpSocket.h"
    #import <ifaddrs.h>
    #import <arpa/inet.h>@interface ViewController () <GCDAsyncUdpSocketDelegate>
    @property (weak, nonatomic) IBOutlet UIButton *open;
    @property (weak, nonatomic) IBOutlet UIButton *close;
    @property (weak, nonatomic) IBOutlet UIButton *op;
    @property (weak, nonatomic) IBOutlet UIButton *opclose;
    @end@implementation ViewControllerstatic BOOL bsw = false;
    GCDAsyncUdpSocket * udpSocket = nil;
    static NSNotificationCenter *center = nil;- (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        //
        if (center == nil) {
            center = [NSNotificationCenter defaultCenter];
            [center addObserver:self selector:@selector(msgSW:) name:@"msgSW" object:nil];
        }
        // socket init
        if(udpSocket == nil){
            udpSocket = [[GCDAsyncUdpSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
            NSError * error = nil;
            [udpSocket bindToPort:5678 error:&error];
            if (error) {
                NSLog(@"udpsocket error:%@",error);
            }else {
                [udpSocket beginReceiving:&error];
                [udpSocket enableBroadcast:YES error:&error];
                NSLog(@"udpsocket ok %@:%i",[self deviceIPAdress],5678);
            }
        }
    }- (void) msgSW:(NSNotification *) notificaion
    {
        if (bsw) {
            bsw = false;
            self.open.hidden = true;
            self.close.hidden = true;
            self.op.hidden = false;
            self.opclose.hidden = false;
        }else{
            bsw = true;
            self.open.hidden = false;
            self.close.hidden = false;
            self.op.hidden = true;
            self.opclose.hidden = true;
        }
    }- (IBAction)Hide:(id)sender {
        if (bsw) {
            bsw = false;
            self.open.hidden = true;
            self.close.hidden = true;
            self.op.hidden = false;
            self.opclose.hidden = false;
        }else{
            bsw = true;
            self.open.hidden = false;
            self.close.hidden = false;
            self.op.hidden = true;
            self.opclose.hidden = true;
        }
    }- (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }#pragma  - GCDAsyncUdpSocket delegate
    -(void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data fromAddress:(NSData *)address withFilterContext:(id)filterContext
    {
        //data就是接收的数据
        unsigned char var;
        
        if (data.length == 1)
        {
            [data getBytes:&var length:1];
            [center postNotificationName:@"msgSW" object:nil];
            NSLog(@"get=%d",var);
        }
    }- (NSString *)deviceIPAdress {
        NSString *address = @"an error occurred when obtaining ip address";
        struct ifaddrs *interfaces = NULL;
        struct ifaddrs *temp_addr = NULL;
        int success = 0;
        
        success = getifaddrs(&interfaces);
        
        if (success == 0) { // 0 表示获取成功
            
            temp_addr = interfaces;
            while (temp_addr != NULL) {
                if( temp_addr->ifa_addr->sa_family == AF_INET) {
                    // Check if interface is en0 which is the wifi connection on the iPhone
                    if ([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
                        // Get NSString from C String
                        address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
                    }
                }
                
                temp_addr = temp_addr->ifa_next;
            }
        }
        
        freeifaddrs(interfaces);
        return address;
    }@end
      

  2.   


    进入b界面的事storyboard界面可视化设置的,无限代码关联。盼高手解答!
      

  3.   

    -(void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
        //按需+判断试试
        [self.view setNeedsDisplay];
    }
      

  4.   

    显示流程和测试流程我再具体描述下:
    app启动----->首页A界面(接收网络命令,能正常隐藏或者显示界面上的按钮布局)---->按跳转到B界面按钮----->进入了B界面---->在B界面按返回按钮----->回到A界面----->在A界面(接收到网络命令“隐藏或者显示界面上的按钮”,但不见命令有效,界面不见刷新,看不到按钮被隐藏或者显示的变化,打了断点,代码执行进入均正常,就不见界面刷新)。
      

  5.   

    遇到这种怪异的问题,很可能是xib或者storybord的问题,建议删了重建试试