2017-02-11 4 views
0

ラベルとジェスチャ認識を使用する基本的なプロジェクトを作成しようとしていますが、少し難しかったです。私は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 
+0

さて、両方のラベルで 'userInteractionEnabled'を' YES'に設定しようとしましたが、違いはありませんでした。しかし、表示されているラベルには、プログラムで設定したテキストは含まれていません。彼らはちょうど "ラベル"と言う。 – trigonoah

+1

そして私は以下の答えでなぜ説明した。 – matt

答えて

1

は、あなたがストーリーボードからself.labelQuestionにラベルを接続したとしましょう。ここまでは順調ですね。起動時に、self.labelQuestionは、ストーリーボードのラベルを指します。これは、実行中のアプリの表示可能なインターフェイスに表示されます。

しかし、その後、あなたが言う:

self.labelQuestion = [[UILabel alloc] init]; 

切断コンセントとは、新しい空白のラベルでその変数(self.labelQuestion)の値を置き換えること!だから、あなたが今何をlabelQuestionにするかは、あなたが接続を壊したので、ストーリーボードのものに影響を与えることはできません。

あなたは4つのラベルすべてに対してそうしているので、4つのアウトレットすべてを粉塵に砕いてしまった。したがって、他のコードはストーリーボードからの可視ラベルには影響しません。

[[UILabel alloc] init]の4行をすべて削除すると、すべて正常に動作します。

+0

答えをありがとう。私は見落としていたニュアンスがあることを知っていました。私は '[[UILabel alloc] init]'ステートメントを取り出し、すべてがスムーズに動作しています。私は2番目の 'userInteractionEnabled'パラメータを追加して100%にする必要がありました。 – trigonoah

関連する問題