Ro4ch
Ro4ch

Reputation: 850

How to change a label text on one ViewController from another ViewController

In viewOne I have labelOne and labelOne has text. I would like labelTwo which is in viewTwo obtain it's text from labelOne(which is in viewOne).

How would I go about making this happen?

More information - I have two separate view controllers being controlled by a UIPageViewController.

Upvotes: 0

Views: 1874

Answers (4)

user3189586
user3189586

Reputation: 125

  • In your storyboard select first view controller then control drag from first view to second view controller

  • Select Push from list you see.

  • Select line that is newly added between two view controllers.

  • Assign name to it.(like "PushSegue")

Added Code in your First view controller on button Click .

[self performSegueWithIdentifier:@"PushSegue" sender:self];

Then add Delegate for PrepareForSegue

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
   if ([[segue identifier] isEqualToString:@"ListMenuSegue"])
   {
    SecondViewController *SecondVC = [segue destinationViewController];
    userViewController.Lable_String=@"Text_to_send";  //You will Get error here for Lable_String ->"Property not found etc" Becuse 
   }  

Add This code in your secondViewController.h

@property(strong,nonatomic)NSString *Lable_String;

Then Print String Value in secondViewController.m

- (void)viewDidLoad 
{
   [super viewDidLoad];
   NSLog(@"%@",Lable_String);
}

Upvotes: 1

Ganee....
Ganee....

Reputation: 302

You can give tags to each label like

labelOne.tag = 1001;

In the viewTwo class, you can get labelOne

UILabel *labelOne = (UILabel *)[self.superview viewWithTag:1001];

This will work if both viewOne, viewTwo are added on same view/view controller.

Upvotes: 0

atrebbi
atrebbi

Reputation: 553

you can add a tag to UILabel , and use

UILable *l = ((UILable*)view.viewWithTag)

to get the label from view

or iterate in its subviews checking if you find a UILabel

for (UIView *subview in view.subviews)
    {
        if ([subview isKindOfClass:[UILable class]) {
             UILable *l =  (UILable *)subview;
        }
    }

Upvotes: 1

Akshay Hastekar
Akshay Hastekar

Reputation: 168

You can do by making a property of label in viewTwo.When you will perform segue in viewOne then assign labelOne text to the label property of viewTwo.In viewTwo didLoad method get text from property label and give these text to labelTwo of viewTwo.

Upvotes: 0

Related Questions