0

This method not working to me. I'm working for minutes. I somehow could not.

This is my code.

- (IBAction)donebutton:(id)sender {
    AddTaskViewController *addtask = [[AddTaskViewController alloc]initWithNibName:@"AddTask" bundle:nil];
    addtask.testlabel.text = self.zaman1.text;
    [self dismissViewControllerAnimated:YES completion:nil];

}

Everything is normal but not working. Is that not so? It is wrong?

Gabriel.Massana
  • 8,165
  • 6
  • 62
  • 81
Salieh
  • 693
  • 2
  • 7
  • 12
  • 2
    What do you expect to happen? At the minute you're creating a view controller then doing absolutely nothing with it. Did you mean to `presentViewController:...`? – Tommy Apr 23 '13 at 23:37
  • I want to be equal to the label.text – Salieh Apr 23 '13 at 23:42
  • @Salieh: Tommy is right may be you are confusing it with `presentViewController`. What exactly you are trying to achieve, can you please elaborate ? – Deepesh Gairola Apr 24 '13 at 00:01
  • I have two label. Label1 in ViewController1 and Label2 in ViewController2. I want to Button click then label2.text = label1.text. It's that simple. But I could not. – Salieh Apr 24 '13 at 00:10

3 Answers3

1

You must assign this string in your viewWillAppear method, IBOutlets (I'm assuming testlabel is a IBOutlet UILabel *) cannot be configured until the view is initialized. If this doesn't help please specify what the error is.

MichaelScaria
  • 1,560
  • 2
  • 16
  • 25
0

A better approach would be to create an NSString* property on AddTaskViewController. You can do it like this:

In AddTaskViewController.h add the following:

@property (nonatomic, strong) NSString* myLabelsText;

then in AddTaskViewController.m be sure to add this in viewWillAppear:

self.testlabel.text = self.myLabelsText;

Now assuming you've declared your testLabel and myLabelsText appropriately and they get synthesized, your view controller will apply the string correctly at the proper time and your function should then be changed to this:

- (IBAction)donebutton:(id)sender {
    AddTaskViewController *addtask = [[AddTaskViewController alloc]initWithNibName:@"AddTask" bundle:nil];

    // Set the value on your new NSString* property and let the view controller handle the rest
    addTask.myLabelsText = self.zaman1.text;

    // Don't you want to 'present' the view controller rather than 'dismiss' after having provided it with data?
    [self dismissViewControllerAnimated:YES completion:nil];

}
Aaron
  • 7,055
  • 2
  • 38
  • 53
0

You should use delegate protocols and instead of setting properties of button on previous view controller just pass a string back. This question should help you in implementing Delegate protocol

dismissModalViewController AND pass data back

Community
  • 1
  • 1
Deepesh Gairola
  • 1,252
  • 12
  • 18