ラベルとジェスチャ認識を使用する基本的なプロジェクトを作成しようとしていますが、少し難しかったです。私は4つのUILabelsのインタフェースをアプリケーション画面の中央に垂直に配置しました。以下のようなものになります。ラベルとジェスチャ認識機能を使用した基本的な例
質問
次の質問
回答
ショー回答
...わずか4ラベルを、次々に配置。私はInterface Builderから私のViewController.h
ファイルに接続して、各ラベルのIBOutletプロパティを作成します。十分に簡単です。次に、すべての実装コードを.m
ファイル(下記参照)に書きます。唯一の問題は、アプリケーションが実行されているときに表示されるのは、Interface Builderのデフォルトの「ラベル」テキストを持つ4つの配置されたラベルです。 2つのラベルに追加したジェスチャ認識機能は、接続/トリガーのようではありません。
誰かが私のために問題に少し明るい光を当てることができました。簡単な運動として始まったことは私を少し怒らせている。
は、ここに私のViewcontroller.hファイルです:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (nonatomic, strong) IBOutlet UILabel *labelQuestion;
@property (nonatomic, strong) IBOutlet UILabel *labelNextQuestion;
@property (nonatomic, strong) IBOutlet UILabel *labelAnswer;
@property (nonatomic, strong) IBOutlet UILabel *labelShowAnser;
@end
そして、ここに私のViewcontroller.mファイルがあります:
#import "ViewController.h"
@interface ViewController()
@property (nonatomic, copy) NSArray *arrayQuestions;
@property (nonatomic, copy) NSArray *arrayAnswers;
@property (assign) int incrementer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// First create the datasource arrays with five elements each
self.arrayQuestions = [[NSArray alloc] init];
self.arrayQuestions = [NSArray arrayWithObjects:@"Who am I?", @"Who are you?", @"Where are we?", @"What's going on?", @"Why is this happening?", nil];
self.arrayAnswers = [[NSArray alloc] init];
self.arrayAnswers = [NSArray arrayWithObjects:@"Damian", @"Dmitri", @"I don't know.", @"You tell me.", @"I have no clue", nil];
// Reset the incrementer's value
self.incrementer = 0;
// Next configure the labels
self.labelQuestion = [[UILabel alloc] init];
self.labelQuestion.text = [self.arrayQuestions objectAtIndex:self.incrementer];
self.labelNextQuestion = [[UILabel alloc] init];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(nextQuestionLabelPressed)];
[tap setNumberOfTapsRequired:1];
[self.labelNextQuestion addGestureRecognizer:tap];
self.labelNextQuestion.text = @"Show next question";
self.labelAnswer = [[UILabel alloc] init];
self.labelAnswer.text = @"?????";
self.labelShowAnser = [[UILabel alloc] init];
self.labelShowAnser.userInteractionEnabled = YES;
UITapGestureRecognizer *tapp = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showAnswerLabelPressed)];
[tapp setNumberOfTapsRequired:1];
[self.labelShowAnser addGestureRecognizer:tapp];
self.labelShowAnser.text = @"Show answer";
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)nextQuestionLabelPressed {
self.incrementer++;
self.labelQuestion.text = [self.arrayQuestions objectAtIndex:self.incrementer];
self.labelAnswer.text = @"?????";
}
- (void)showAnswerLabelPressed {
self.labelAnswer.text = [self.arrayAnswers objectAtIndex:self.incrementer];
}
@end
さて、両方のラベルで 'userInteractionEnabled'を' YES'に設定しようとしましたが、違いはありませんでした。しかし、表示されているラベルには、プログラムで設定したテキストは含まれていません。彼らはちょうど "ラベル"と言う。 – trigonoah
そして私は以下の答えでなぜ説明した。 – matt