2016-08-15 74 views
0

我试图设置UIButton来通过IOS应用程序控制Raspberry PI上的GPIO引脚。“使用未声明标识符'holdDown'”错误

我遇到了UIButton的问题,并得到一些错误“使用未声明的标识符self”以及“期望的标识符或'(”和一个错误“初始化器元素不是编译时常量。” 我的代码是下面

// 

#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 , [email protected]"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 
} 

下面是米y头文件。

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

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

+0

为什么''UIControlEventTouchUpInside它自己的上线(它看起来像它应该是'addTarget内:forControlEvents:'上一行)?为什么' - (void)holdDown'没有右大括号?为什么' - (void)holdRelease'没有右括号?您应该使用Storyboard,以便在编写UI时不会犯错。 –

+0

你是对的,UIControlEventTouchUpInside应该在前一行。我的错。再次,当我将代码移动到右括号时,它们被移动了一行。对困惑感到抱歉。 –

+0

现在,我不明白你发布的代码。对于'valveToggle'的所有3个语句都应该在一个方法的代码块中。请在错误周围发布完整的方法。 –

回答

-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我相信你的问题是不明确的。

+0

对不起,如果我的问题我很困惑,为什么我得到一个“使用未声明的标识符'holdDown'错误的行” - (void)holdDown {“这是因为我没有声明在我的.h文件中的东西? –

+0

你不喜欢不需要在Header(.h)文件中声明函数,直到除非你想从其他控制器访问该函数为止。做一些必要的步骤。 1.清理代码。 2。如果可能的话重新启动Xcode,甚至是Mac。 3.再试一次。 如果你确定方法定义,那么我相信不会有问题。 请确保您的功能完美定义。即 - (void)holdDown {} –

+0

我将如何定义 - (void)holdDown {}?是否在我的.h文件中放入 - (void)holdDown {},然后在{}中定义它的含义?我认为“holdDown”是一个来自UIButton库的函数?这段代码没有定义这个函数是什么? [_valveToggle addTarget:self action:@selector(holdDown)forControlEvents:UIControlEventTouchDown];我很迷茫,你可以看到!任何如何做到这一点的例子将不胜感激! –

0

问题与我在评论中描述的一样。你有三行代码应该在一个方法中,它们自己放在ViewController.m文件中。这些线路不能自己:

UIButton *valveToggle = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

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

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

它看起来像你试图用这些行创建你的用户界面的一部分;我的建议是你只是使用Storyboard而不是使用代码。如果你想继续这些行,然后将它们粘在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]; 
} 
+0

这很有道理。让我今晚尝试一下,看看我得到了什么。感谢您的耐心和帮助! –