“使用未声明的标识符'holdDown'”错误

时间:2016-08-15 14:15:43

标签: ios objective-c

我正在尝试通过IOS应用程序设置UIButton来控制Raspberry PI上的GPIO引脚。

我遇到了UIButton的问题,并获得了一些错误“使用未声明的标识符'self”以及“期望的标识符或'(”和错误“Initializer元素不是编译时常量”。 我的守则在下面

   //

#import "ViewController.h"


@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self initNetworkCommunication];

    //used so that when you minimize the app it inits the the network again.
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(applicationEnteredForeground:)
                                                 name:UIApplicationWillEnterForegroundNotification
                                               object:nil];


    _logo.layer.cornerRadius = _logo.frame.size.width / 2;
    _logo.clipsToBounds = YES;
    _logo.layer.borderColor = [[UIColor whiteColor] CGColor];
    _logo.layer.borderWidth = 6;

    self.view.backgroundColor = [UIColor colorWithRed:(2/255.0f) green:(160/255.0f) blue:(224/255.0f) alpha:1];
}

- (void)applicationEnteredForeground:(NSNotification *)notification {
    [self initNetworkCommunication];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)initNetworkCommunication {
    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"192.168.1.50", 7777, &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];

}


- (IBAction)toggleValve:(id)sender {

    UISegmentedControl*button = ((UISegmentedControl*)sender);
    long tag = button.tag;

    NSString *response  = [NSString stringWithFormat:@"P%ld%@", tag , button.selectedSegmentIndex?@"L" : @"H"];
    NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];
    [_outputStream write:[data bytes] maxLength:[data length]];

}

UIButton *valveToggle = [UIButton buttonWithType:UIButtonTypeRoundedRect];

[valveToggle addTarget:self  action:@selector(holdDown)forControlEvents:UIControlEventTouchDown];

[valveToggle addTarget:self action:@selector(holdRelease) forControlEvents: UIControlEventTouchUpInside];

-(void) holdDown{
    NSLog(@"hold Down");
    //Set GPIO High
}
-(void)holdRelease {
    NSLog(@"hold release");
    //Set GPIO Low
}


- (IBAction)shutdown:(id)sender {

    NSData *data = [[NSData alloc] initWithData:[@"shutdown" dataUsingEncoding:NSASCIIStringEncoding]];
    [_outputStream write:[data bytes] maxLength:[data length]];

}

- (IBAction)reboot:(id)sender {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"REBOOT?" message:@"Are you sure you want to reboot?" delegate:self cancelButtonTitle:@"OH.. Hell No!" otherButtonTitles:@"I said REBOOT!", nil];

    [alert show];

}

- (void)alertView:(UIAlertController *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    if(buttonIndex == 1){

        NSData *data = [[NSData alloc] initWithData:[@"reboot" dataUsingEncoding:NSASCIIStringEncoding]];
        [_outputStream write:[data bytes] maxLength:[data length]];
        NSLog(@"YES %@",data);
    }
}

- (IBAction)reset:(id)sender {
    [self initNetworkCommunication];
}
 @end

以下是错误代码

的图片

screen shot of Error codes

The code block is 
UIButton *valveToggle = [UIButton buttonWithType:UIButtonTypeRoundedRect];

[[valveToggle addTarget:self  action:@selector(holdDown)forControlEvents:UIControlEventTouchDown];

[[valveToggle addTarget:self action:@selector(holdRelease) forControlEvents: UIControlEventTouchUpInside];

-(void) holdDown{
    NSLog(@"hold Down");
    //Set GPIO High
}
-(void)holdRelease {
    NSLog(@"hold release");
    //Set GPIO Low
}

以下是我的Header文件。

// //  ViewController.h //  RocketOne // //  Created by Christopher      Beck
on 8/9/2559 BE. //  Copyright © 2559 BE Christopher Beck. All rights              reserved. //

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<NSStreamDelegate> {
}

@property (nonatomic, retain) NSInputStream *inputStream;
@property  (nonatomic, retain) NSOutputStream *outputStream;
@property (weak,nonatomic) IBOutlet UIButton *valveToggle;
@property (weak, nonatomic)IBOutlet UIImageView *logo;

- (IBAction)ToggleValve:(id)sender;
- (IBAction)shutdown:(id)sender;
- (IBAction)reboot:(id)sender;
- (IBAction)reset:(id)sender;



@end

我们将非常感谢您解决此错误的任何帮助!我是新手,猜测它很简单。

1 个答案:

答案 0 :(得分:-1)

我认为有些代码无效,这对你的事业没有帮助......

// Improved code are given below,  
UIButton *valveToggle = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

[valveToggle addTarget:self  action:@selector(holdDown)forControlEvents:UIControlEventTouchDown];

[valveToggle addTarget:self action:@selector(holdRelease) forControlEvents: UIControlEventTouchUpInside];

-(void) holdDown{
        NSLog(@"hold Down");
       //Set GPIO High 
}
-(void)holdRelease {
    NSLog(@"hold release");
    //Set GPIO Low 
}
P.S我相信你的问题不明确。