【发布时间】: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
这是错误代码的图片
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