我之前发布了一个关于同一件事的问题,但现在我做了一个简单的项目来展示我在做什么,这样可以更容易地找到问题。
我有两个 viewController,一个叫做 ViewController,另一个叫做 SecondViewController。我尝试将一个名为 testy 的 NSString 发送到 viewController 并记录它,但它返回 null。
这是我的代码尝试将字符串从 viewController 发送到 secondViewController
视图控制器.m
#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize cellName;
- (void)viewDidLoad
{
[super viewDidLoad];
cellName = @"testy";
SecondViewController *obj = [[SecondViewController alloc] init];
obj.cellName2 = self.cellName;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
视图控制器.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
}
@property(nonatomic,retain) NSString *cellName;
@end
第二视图控制器.m
#import "SecondViewController.h"
#import "ViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
@synthesize cellName2;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidAppear:(BOOL)animated {
NSLog(@"%@",cellName2);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
SecondViewController.h
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController {
}
@property(nonatomic,retain) NSString *cellName2;
@end
编辑
我想说我的故事板有两个 viewController,每个都有一个按钮。每个按钮以模态方式将您带到另一个视图。