动态分配文本到标签
Assigning Text to Labels Dynamically
我让用户通过文本字段输入文本,然后将其显示在单独 ViewController 的标签中。
基本上我需要做的是
1 - 让用户输入文本
2 - 第二个 ViewController 创建文本并将其分配给标签
我需要用户输入多个数据条目,因此需要在用户输入数据时动态创建标签。
我正在使用委托通过 ViewControllers 传递我的数据。
感谢您的帮助!
ViewController1
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Pass duration and time of day through to dataHistory
if ([[segue identifier] isEqualToString:@"dataViewSegue"])
{
dataHistory *dataHistory = [segue destinationViewController];
dataHistory.duration = self.durationTextField.text;
dataHistory.timeOfDay = self.userChoice;
}
}
ViewController2
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
if ([durationLabel.text isEqual: @""])
{
[durationLabel setHidden:YES];
[timeOfDayLabel setHidden:YES];
[editButton setHidden:YES];
}
self.durationLabel.text = [self.duration description];
self.timeOfDayLabel.text = [self.timeOfDay description];
}
为什么不考虑NSUserDefaults
?它甚至可以跨会话传递数据。
传递数据:
- (IBAction)saveLabel:(id)sender {
NSArray *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"DATA"];
NSMutableArray *currentDataArray;
if (data == nil) {
currentDataArray = [[NSMutableArray alloc]init];
} else {
currentDataArray = [[NSMutableArray alloc]initWithArray:data];
}
[currentDataArray addObject:self.textField.text];
[[NSUserDefaults standardUserDefaults] setObject:currentDataArray forKey:@"DATA"];
}
处理数据:(在另一个视图控制器中)
- (void) viewWillAppear:(BOOL)animated {
NSArray *dataArray = [[NSUserDefaults standardUserDefaults]objectForKey:@"DATA"];
if (dataArray != nil) {
int count = 0;
for (NSString *text in dataArray) {
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 200 + 40 * count, self.view.frame.size.width, 40)];
label.text = text;
[self.view addSubview:label];
count++;
}
}
}
找到演示
我让用户通过文本字段输入文本,然后将其显示在单独 ViewController 的标签中。
基本上我需要做的是
1 - 让用户输入文本
2 - 第二个 ViewController 创建文本并将其分配给标签
我需要用户输入多个数据条目,因此需要在用户输入数据时动态创建标签。
我正在使用委托通过 ViewControllers 传递我的数据。
感谢您的帮助!
ViewController1
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Pass duration and time of day through to dataHistory
if ([[segue identifier] isEqualToString:@"dataViewSegue"])
{
dataHistory *dataHistory = [segue destinationViewController];
dataHistory.duration = self.durationTextField.text;
dataHistory.timeOfDay = self.userChoice;
}
}
ViewController2
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
if ([durationLabel.text isEqual: @""])
{
[durationLabel setHidden:YES];
[timeOfDayLabel setHidden:YES];
[editButton setHidden:YES];
}
self.durationLabel.text = [self.duration description];
self.timeOfDayLabel.text = [self.timeOfDay description];
}
为什么不考虑NSUserDefaults
?它甚至可以跨会话传递数据。
传递数据:
- (IBAction)saveLabel:(id)sender {
NSArray *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"DATA"];
NSMutableArray *currentDataArray;
if (data == nil) {
currentDataArray = [[NSMutableArray alloc]init];
} else {
currentDataArray = [[NSMutableArray alloc]initWithArray:data];
}
[currentDataArray addObject:self.textField.text];
[[NSUserDefaults standardUserDefaults] setObject:currentDataArray forKey:@"DATA"];
}
处理数据:(在另一个视图控制器中)
- (void) viewWillAppear:(BOOL)animated {
NSArray *dataArray = [[NSUserDefaults standardUserDefaults]objectForKey:@"DATA"];
if (dataArray != nil) {
int count = 0;
for (NSString *text in dataArray) {
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 200 + 40 * count, self.view.frame.size.width, 40)];
label.text = text;
[self.view addSubview:label];
count++;
}
}
}
找到演示