我试图创建一个使用标签和手势识别的基本项目但是遇到了一些困难。我创建了一个4个UILabel的界面,沿着应用程序屏幕的中心垂直放置。看起来像:
问题
下一个问题
答案
显示答案
...只有4个标签,一个接一个地定位。我从Interface Builder连接到我的ViewController.h
文件,为每个标签创建IBOutlet属性。很简单。然后我继续编写.m
文件中的所有实现代码(如下所示)。唯一的问题是当应用程序运行时,显示的所有内容都是四个定位标签,默认为"标签" Interface Builder中的文本。我添加到两个标签中的手势识别器似乎也没有连接/触发。
希望有人能为我解决这个问题。最初只是一个简单的练习让我有点生气。
这是我的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 :(得分:1)
假设您已将故事板中的标签与self.labelQuestion
相关联。到现在为止还挺好。当您启动时,self.labelQuestion
指向故事板中的标签,该标签是您在正在运行的应用程序的可见界面中看到的标签。
然后你说:
self.labelQuestion = [[UILabel alloc] init];
断开连接插座,用新的空白标签替换该变量(self.labelQuestion
)的值!因此,您现在对labelQuestion
所做的任何事情都不可能影响故事板中的那个,因为您已断开连接。
你为所有四个标签做到这一点,所以你把所有四个出口都粉碎成灰尘。因此,您的其他代码对来自故事板的可见标签没有影响。
只需删除所有四条[[UILabel alloc] init]
行,一切都会好的。