找到你要的答案

Q:Assigning Text to Labels Dynamically

Q:动态分配文本到标签

I am having a user enter in text through text field and then display it in a label in a separate ViewController.

Basically what I need to happen is

1 - Have the user enter in text

2 - The second ViewController creates and assigns the the text to a label

I need to the user to enter in multiple data entries and therefore need the labels to be created dynamically when the data is entered from the user.

I am passing my data through ViewControllers using delegates.

Thanks for the help!

我有一个用户输入文本通过文本字段,然后显示在一个标签在一个单独的视图。

基本上我需要发生的是

1 -让用户输入文本

2第二视图创建和指定的文本标签

我需要用户输入多个数据项,因此需要从用户输入数据时动态创建标签。

我是通过我的数据通过使用代表ViewControllers。

谢谢你的帮助!

answer1: 回答1:

Why not consider NSUserDefaults? It can even pass the data across sessions.

Pass the data:


- (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"];
}

Process the data: (in another view controller)


- (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++;
        }
    }
}

Demo can be found at https://github.com/null09264/Demo-Stack

为什么不考虑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++;
        }
    }
}

演示可以发现在https://github.com/null09264/demo-stack

answer2: 回答2:

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];
}

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];
}
ios  objective-c  uilabel