0

我可以在我的主视图中将标签链接到 ViewController.h。但是,在设置屏幕中,程序不会让我将标签输出到 ViewController.h。因此,下面的“testLabel”被标记为未声明的标识符。我希望标签反映用户是否连接到互联网。

视图控制器.h

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import "Reachability.h"

@interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
    Reachability *internetReachableFoo;
}

- (IBAction)cameraButtonClicked:(id)sender;
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UILabel *testLabel; //Not linked

@end

视图控制器.m

- (void)testInternetConnection
{
    internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"];

    // Internet is reachable
    internetReachableFoo.reachableBlock = ^(Reachability*reach)
    {
        // Update the UI on the main thread
        dispatch_async(dispatch_get_main_queue(), ^{
            testLabel.text = @"YAY Intrawebs!!";
        });
    };

    // Internet is not reachable
    internetReachableFoo.unreachableBlock = ^(Reachability*reach)
    {
        // Update the UI on the main thread
        dispatch_async(dispatch_get_main_queue(), ^{
            testLabel.text = @"Someone broke the internet";
        });
    };

    [internetReachableFoo startNotifier];
}

设定画面

4

1 回答 1

0

您是否连接了情节提要中的标签?完成后...您将必须通过以下方式引用 testLabel

_testLabel

或者

self.testLabel

May 将不得不对标签进行弱引用,因为如果它在一个块中。像这样:

// Update the UI on the main thread
__block __weak ViewController *weakself = self;
dispatch_async(dispatch_get_main_queue(), ^{
    weakself.testLabel.text = @"YAY Intrawebs!!";
});
于 2013-10-14T14:51:52.117 回答