0

我是使用故事板进行 Xcode 编程的新手。我正在尝试将一个值(标签的文本)从“SecondViewController”传递给“View Controller”。这是我的代码:

视图控制器.m

- (IBAction)showSecondView:(id)sender {
    NSLog(@"Trying..");
    // The identifier used here is set on the second view controller in the UIStoryboard.
    SecondViewController *svc = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondIdentifier"];
    NSLog(@"Test: %@",svc.labelTest.text);  //this print NIL!!
    svc.labelTest.text = @"GOOFY!!";
    [self.navigationController pushViewController:svc animated:YES];
}

SecondViewController.h

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

- (IBAction)returnToFirstView:(id)sender;
- (IBAction)toTabBar:(id)sender;
@property (strong, nonatomic) IBOutlet UILabel *labelTest;

@end

第二视图控制器.m

#import "SecondViewController.h"

@implementation SecondViewController
@synthesize labelTest;

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

有人可以帮助我吗?非常感谢!

斯特凡诺

4

1 回答 1

1

如果您正在使用情节提要,为什么不使用- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

替换你的- (IBAction)showSecondView:(id)sender {-method

和:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"SecondIdentifier"]) {
SecondViewController *svc = (SecondViewController *)segue.destinationViewController;
svc.labelTest.text = @"Goofy";
}
于 2012-04-29T11:33:35.737 回答