我是使用storyboard进行Xcode编程的新手。 我正在尝试将值(标签的文本)传递给“SecondViewController”中的“View Controller”。 这是我的代码:
ViewController.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
SecondViewController.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;
}
有人可以帮我吗? 非常感谢你!
斯特凡诺
答案 0 :(得分:1)
如果您使用的是故事板,为什么不使用- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
替换您的- (IBAction)showSecondView:(id)sender {
- 方法
with:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"SecondIdentifier"]) {
SecondViewController *svc = (SecondViewController *)segue.destinationViewController;
svc.labelTest.text = @"Goofy";
}