【问题标题】:"Use of Undeclared Identifier 'holdDown'" Error“使用未声明的标识符‘holdDown’”错误
【发布时间】:2016-08-15 14:15:42
【问题描述】:

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

我遇到了 UIButton 的问题,并且出现了一些错误“使用未声明的标识符 'self”以及“预期的标识符或 '(”以及错误“Initializer element is not a compile-time Constant”。 我的代码如下

   //

#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
}

下面是我的头文件。

// //  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

对于解决此错误的任何帮助将不胜感激!我对此很陌生,猜测这很简单。

【问题讨论】:

  • 为什么UIControlEventTouchUpInside 在它自己的一行上(看起来它应该在上一行的addTarget: forControlEvents: 内)?为什么- (void) holdDown 没有右大括号?为什么- (void)holdRelease 没有右大括号?您可能应该使用 Storyboard,这样您在编写 UI 时就不会出错。
  • 你是正确的 UIControlEventTouchUpInside 应该在前一行。我的错。当我将代码移到右括号上时,再次向上移动了一行。很抱歉造成混乱。
  • 就目前而言,我不理解您发布的代码。 valveToggle 的所有 3 条语句都应位于方法的代码块中。请发布您围绕错误的完整方法。
  • 我用所有代码更新了问题。现在看来,“使用未声明的标识符'self”和“预期的标识符或'(”出现错误,并且出现错误“Initializer element is not a compile-time Constant。”我正在关注本教程bitcows.com/?p=249并尝试将其从分段控制按钮更改为类似于瞬时开关的取消按钮,但我似乎无法弄清楚。谢谢您的帮助!

标签: ios objective-c


【解决方案1】:

问题正如我在 cmets 中所描述的那样。您在 ViewController.m 文件中拥有三行代码,它们应该在一个方法中。这些行不能单独存在:

UIButton *valveToggle = [UIButton buttonWithType:UIButtonTypeRoundedRect];

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

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

您似乎正在尝试使用这些行创建 UI 的一部分;我的建议是您只使用情节提要而不是使用代码。如果您想继续使用这些行,请将它们粘贴在 viewDidLoad 中(但有关更多信息,请参阅此 SO 帖子:Which should I use, -awakeFromNib or -viewDidLoad?)并将按钮添加到 UI。

例如:

- (void)viewDidLoad:(BOOL)animated {
    [super viewDidLoad];

    // Now your code is inside a method
    UIButton *valveToggle = [UIButton buttonWithType:UIButtonTypeRoundedRect];

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

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

    // You are missing a button title, a frame and adding the button to the UI - you should use a Storyboard instead
    [valveToggle setTitle:@"Toggle Valve" forState:UIControlStateNormal];
    button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0); // Example size - set correctly but prefer constraints
    [self.view addSubview:valveToggle];
}

【讨论】:

  • 这是有道理的。让我今晚试试,看看我能得到什么。感谢您的耐心和帮助!
【解决方案2】:

我认为有些代码钝器无助于您的事业......

// 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 我相信你的问题不清楚。

【讨论】:

  • 对不起,如果我的问题令人困惑。我很困惑为什么我在带有“-(void)holdDown{”的行中收到“使用未声明的标识符'holdDown”错误这是因为我没有在我的 .h 文件中声明某些内容吗?
  • 除非您想从其他控制器访问它,否则您不需要在 Header (.h) 文件中声明函数。做一些必要的步骤。 1.清理代码。 2. 如果可能的话重新启动 Xcode,甚至是 Mac。 3. 再试一次。如果您对方法定义没问题,那么我相信不会有问题。请确保您的功能定义完美。即 -(void)holdDown{}
  • 如何定义 -(void)holdDown{}?我是否将 -(void)holdDown{} 放在我的 .h 文件中,然后在 {} 中定义它的内容?我认为“holdDown”是 UIButton 库中的一个函数?这段代码没有定义函数是什么吗? [_valveToggle addTarget:self action:@selector(holdDown)forControlEvents:UIControlEventTouchDown];如您所见,我很迷茫!任何有关如何完成此操作的示例将不胜感激!
  • 好吧,我想我明白了,但是当我尝试定义函数时,我仍然收到“使用未声明的标识符按住”?我应该在其他地方定义它吗?
猜你喜欢
  • 1970-01-01
  • 2013-03-12
  • 2014-08-04
  • 2020-05-05
  • 2019-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多