我试图设置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
这里是错误代码的图片
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
任何帮助解决此错误将不胜感激!我对此很陌生,猜测这很简单。
为什么''UIControlEventTouchUpInside它自己的上线(它看起来像它应该是'addTarget内:forControlEvents:'上一行)?为什么' - (void)holdDown'没有右大括号?为什么' - (void)holdRelease'没有右括号?您应该使用Storyboard,以便在编写UI时不会犯错。 –
你是对的,UIControlEventTouchUpInside应该在前一行。我的错。再次,当我将代码移动到右括号时,它们被移动了一行。对困惑感到抱歉。 –
现在,我不明白你发布的代码。对于'valveToggle'的所有3个语句都应该在一个方法的代码块中。请在错误周围发布完整的方法。 –