在另一个UIViewController上执行方法

时间:2013-08-23 12:38:57

标签: ios objective-c storyboard

我正在使用以下代码在 SViewController 中设置 UILabel 的文本,从另一个ViewController FViewController

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

setTextForLabel方法:

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

我已在 .h文件中声明textLabel的属性如下: @property (nonatomic, retain) IBOutlet UILabel *textLabel; 我还在故事板中设置了IBOutlet。

现在在控制台中我看到“文本是1”,但UI没有显示任何内容! 我做错了什么?

4 个答案:

答案 0 :(得分:0)

删除此行

textLabel = [[UILabel alloc] init];

此外,您需要将界面上的UILabel链接到@property(非原子,保留)IBOutlet UILabel * textLabel;

或者您可以手动添加此标签进行查看      textLabel = [[UILabel alloc] init];      [textLabel setText:[NSString stringWithFormat:@“%@”,[text stringValue]]];

 [self.view addSubView:textLabel];

答案 1 :(得分:0)

我确实喜欢这个

#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

Storyboard

还要检查故事板中的标签属性是否已正确设置

答案 2 :(得分:0)

您的问题与SViewController的视图生命周期相关联。当您致电[.. setTextForLabel]时,您不知道控制器是否已加载其视图(因此您的UILabel很可能尚未创建)

您应该在控制器上使用NSString属性,使用所需文本进行设置,并将其分配给SViewController的viewDidLoad中的UILabel。

在SViewController.h中

@propery (strong, nonatomic) NSString *textToDisplay;

在SViewController.m中

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

答案 3 :(得分:0)

首先去吧

[vc setTextForLabel:[NSNumber numberWithInt:1]]; 
按下vc之后

如果您的标签上写有内容,请在您的xib / storyboard中删除它,将其留空。 如果这不起作用...在推送之前保留setText方法(当你初始化时)并创建一个NSNumber成员,它将保存你的值并将其应用于viewDidAppear,如此

-(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]]];
}