0

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.

Ro4ch
  • 850
  • 8
  • 21
  • With labels, I'd probably just create a helper method in the ViewController that updates both labels at the same time and use to set the values so they always stay in sync. If you are using a UITextField, you could also consider tying into the UITextFieldDelegate to update the second field anytime the first field is updated. – Kyle Mar 29 '16 at 08:11
  • in viewTwo class lableTwo.text = viewOne.lableOne.text – sourav Mar 29 '16 at 08:18
  • Are both these views in the same view controller or are they in different view controllers? What's your view and view controller hierarchy? – Pradeep K Mar 29 '16 at 08:19
  • They are in two separate view controlers. – Ro4ch Mar 29 '16 at 09:22
  • Hi. Could you please add the code on how you are setting these viewControllers to the pageController. – GoGreen Apr 05 '16 at 05:39
  • @GoGreen - I have implemented this from GitHub - https://github.com/derekleeapp/StoryboardPagingDemo there is a RootViewController and a BaseViewController that manages the PageViewController – Ro4ch Apr 05 '16 at 08:23
  • since you are setting labels and you want to pass information between view controllers I am assuming you have created properties and you are using custom viewControllers right? Could you please name the two view controllers for which you want to set the label. – GoGreen Apr 05 '16 at 10:28
  • @GoGreen so the First ViewController is called "LandingViewController" and the Second one is called "ViewController". In the RootViewController.m I have named them [@"landingVC", @"camController" ----- (camController is the ViewController). – Ro4ch Apr 05 '16 at 17:45
  • 1
    okay. so create an `NSString` property in both viewControllers which stores the value of the label you want to display. I saw two methods in rootViewController namely `goToPreviousContentViewController` and `goToNextContentViewController`. check if the viewController you retrieve is of the type `camController`. If so, set the corresponding property of the `camController` with the value of the property in `landingVC`. That should do it. – GoGreen Apr 06 '16 at 05:13
  • 1
    Also @Ro4ch. This is a very basic question in objective C. Passing data between viewControllers is as simple as mentioned by http://stackoverflow.com/a/36280799/2954866. `prepareForSegue` is only a callback showing that the view scene is about to change. For your application, the methods `goToPreviousContentViewController` and `goToNextContentViewController` serves as a callback. The underlying way of passing data remains the same in both cases. – GoGreen Apr 06 '16 at 05:18
  • 1
    Please go through http://stackoverflow.com/a/9736559/2954866 for a better understanding. – GoGreen Apr 06 '16 at 05:19
  • @GoGreen I am going to go through that. Thanks for the help! – Ro4ch Apr 06 '16 at 06:46

4 Answers4

1

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;
        }
    }
atrebbi
  • 553
  • 3
  • 20
1
  • 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);
}
user3189586
  • 125
  • 1
  • 8
  • Thanks for the help. I forgot to mention I have two viewcontroller being controlled by a UIPageViewController. Do you a possible solution when using the the UIPageViewController and not segue? – Ro4ch Mar 29 '16 at 11:12
0

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.

  • Thanks for the response. I added more information to my question but I have two view controllers being controlled by a UIPageViewController and there I have no segues present. – Ro4ch Mar 29 '16 at 11:16
  • Then you have another option which is to make label property in shared class and in viewOne assign labelOne's text to label of shared class and access that shared class's label's text throughout the app. – Akshay Hastekar Mar 29 '16 at 11:18
0

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.

Ganee....
  • 302
  • 2
  • 13