Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling method from another class doesn't work properly

I am trying to change the text and the position of a UILabel from another class.

I have successfully managed to call my -changeLabel method, which is in my FirstViewController class, from my SecondViewController class. Here is the code I have used in my two classes:

SecondViewController:

#import "FirstViewController.h"

@interface SecondViewController ()
@property (nonatomic,strong) FirstViewController *firstViewController;
@end

@implementation SecondViewController
@synthesize firstViewController;

- (IBAction)button:(id)sender {
    firstViewController = [[FirstViewController alloc] init];
    [firstViewController changeLabel];
}

FirstViewController:

- (void)changeLabel {
    NSLog(@"changeLabel is called!");
    label.text = @"New text.";
    label.frame = CGRectMake(10, 100, 150, 40);
    NSLog(@"%@", label.text);
    NSLog(@"%f", label.frame.size.width);
}

The weird thing is that the logger looks like this after pressing the "button" that calls the method:

"2013-12-30 19:24:50.303 MyApp[655:70b] changeLabel is called!"
"2013-12-30 19:24:50.305 MyApp[655:70b] New text."
"2013-12-30 19:24:50.308 MyApp[655:70b] 0.000000"

So it seems the label text change, but it doesn't show up on the screen. And the label width is logged as 0.0.. even though I just set it to 150.

Why is this happening? Am I not able to change frame variables from another class? Is there another way to do this?

IMPORTANT:

As the FirstViewController is the main view controller while the SecondViewController is a side menu, similar to the facebook app:

enter image description here

I want to be able to press a "button" on the SecondViewController(side menu) and call a method in the FirstViewController(main) that changes the position(frame) of a UILabel.

EDIT:

Here is how I created the UILabel:

label = [[UILabel alloc] init];
label.frame = CGRectMake(0, 0, 150, 40);
label.text = @"Text."
[self.view addSubview:label];
like image 282
Peter Avatar asked Sep 21 '26 17:09

Peter


2 Answers

I think problem is this. You are calling method from new instance of FirstViewController.

Let assume

1. FirstViewController at stack[0].
2. SecondViewController at stack[1].

If you are navigating or moving from

FirstViewController->SecondViewController

In this case FirstViewController already in memory with some address 0x23ffff.

And in SecondViewController you are again creating new instance of FirstViewController which is point to another address '0x234jurhu`

- (IBAction)button:(id)sender {
    firstViewController = [[FirstViewController alloc] init];
    [firstViewController changeLabel];
}

Don't create new instance here.

You can use delegate or NSNotification concept for this.

like image 107
Bhumeshwer katre Avatar answered Sep 24 '26 06:09

Bhumeshwer katre


How are you displaying FirstViewController?

Here is the issue:

firstViewController = [[FirstViewController alloc] init];
[firstViewController changeLabel];

You creating a new instance of FirstViewController and updating the label text. If your using these VC's in a navigation stack and you pop back to FirstViewController from SecondViewController, you won't see any label change because they are different instances of the class.

If your using FirstViewController as a childViewController of SecondViewController (with naming of them I don't think this what your doing), then in the - (IBAction)button:(id)sender method you don't need to instantiate a new instance of FirstViewController on each button press.

like image 21
random Avatar answered Sep 24 '26 06:09

random



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!