Performing method on another UIViewController

I am using following code to set the text of a UILabel in SViewController, From another ViewController FViewController

SViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"SViewController"];
[vc setTextForLabel:[NSNumber numberWithInt:1];];
[self.navigationController pushViewController:vc animated:YES];

The setTextForLabel method:

-(void) setTextForLabel: (NSNumber *)text {
      textLabel = [[UILabel alloc] init];
      [textLabel setText:[NSString stringWithFormat:@"%@",[text stringValue]]];
      NSLog(@"The text is %@", textLabel.text);
}

I have declared the property for textLabel in .h file is as follows: @property (nonatomic, retain) IBOutlet UILabel *textLabel; And I have also set the IBOutlet in storyboard.

Now in console I see "The text is 1", but the UI is not showing anything! What am I doing wrong?

Answers


remove this line

textLabel = [[UILabel alloc] init];

Also, you need to link the UILabel on interface to your @property (nonatomic, retain) IBOutlet UILabel *textLabel;

Or you may add this label manually to view textLabel = [[UILabel alloc] init]; [textLabel setText:[NSString stringWithFormat:@"%@",[text stringValue]]];

 [self.view addSubView:textLabel];

First of all go put

[vc setTextForLabel:[NSNumber numberWithInt:1]]; 

after you push the vc.

In your xib/storyboard if your label has something written in it, remove it, leave it blank. If that doesn't work ... leave the setText method before the push one (as you wrote it initialy) and create a NSNumber member that will hold your value and apply it in viewDidAppear like so

-(void) setTextForLabel: (NSNumber *)text{
numberValue = text;  // declared in .h as NSNumber *numberValue (only make it a property if you need acces to it from outside your class)
}

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated]; //this is important
[textLabel setText:[NSString stringWithFormat:@"%@",[numberValue stringValue]]];
}

Your problem is linked to the view lifecycle of SViewController. When you call [.. setTextForLabel] you don't know if the controller has loaded its views (And thus your UILabel have a good chance of not being created yet)

What you should do is use a NSString property on your controller, set it with the desire text and assigned it to the UILabel in the viewDidLoad of SViewController.

in SViewController.h

@propery (strong, nonatomic) NSString *textToDisplay;

in SViewController.m

-(void)viewDidLoad
{
    [super viewDidLoad];
    // Here you know all Outlet are loaded and connected
    textLabel.text = textToDisplay
}

I did like this

#import "FViewController.h"
#import "SViewController.h"
@interface FViewController ()

@end

@implementation FViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}


- (IBAction)do:(id)sender
{
    SViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"SViewController"];
    [self.navigationController pushViewController:vc animated:YES];
     [vc setTextForLabel:[NSNumber numberWithInt:1]];
}

// in SViewController.h

#import <UIKit/UIKit.h>

@interface SViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *label;
-(void) setTextForLabel: (NSNumber *)text;
@end


 // SViewController.m file
#import "SViewController.h"

@interface SViewController ()

@end


@implementation SViewController
@synthesize label;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void) setTextForLabel: (NSNumber *)text{
 //   label.text = [text stringValue];
    NSLog(@"%@",label);
    [label setText:[text stringValue]];
}

@end

also check your label properties in storyboard are properly set


Need Your Help

How can I make my R session vanilla?

r environment

This is a follow up for clarification of a previous question, How can I ensure a consistent R environment among different users on the same server?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.