我有一个名为GettingHere的NSObject,它有一个NSString *内容。
然后我有一个UIViewController,我在其上以编程方式创建一个按钮,如下所示(此按钮按预期工作):
byAirButton = [UIButton buttonWithType:UIButtonTypeCustom];
byAirButton.tag = 1;
byAirButton.frame = CGRectMake(25, 140, 280.f, 40.f);
UIImage *airButton = [UIImage imageNamed:@"gettingHereByAirButton.png"];
[byAirButton setBackgroundImage:airButton forState:UIControlStateNormal];
[self.view addSubview:byAirButton];
[byAirButton addTarget:self action:@selector(byAirButtonClicked) forControlEvents:UIControlEventTouchUpInside];
对于动作:@selector(byAirButtonClicked),我执行以下操作。 gettingHere是GettingHere对象的一个实例。
- (void) byAirButtonClicked
{
gettingHere.content = @"This is how to get here by Air";
NSLog(@"Content: %@", gettingHere.content);
[self performSegueWithIdentifier:@"gettingHereSegue" sender:self];
}
我的想法是为我的GettingHere对象设置内容,然后在用户单击byAirButton时从下一个视图(GettingHereViewController)调用它。 此NSLog显示正在设置内容。
在我的prepareForSegue中,我执行以下操作:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"gettingHereSegue"])
{
NSLog(@"Content to be passed: %@", gettingHere.content);
GettingHereViewController *vc = (GettingHereViewController *)segue.destinationViewController;
vc.gettingHere.content = gettingHere.content;
}
}
segue工作正常,但NSLog将我的gettingHere对象值显示为(null)。
谁能告诉我哪里出错了?我已经多次介入,但无法弄清楚我哪里出错了。
编辑:这是我实例化GettingHere对象的方法。
在SubNavViewController.h
中#import "GettingHereContent.h"
@interface SubNavViewController : UIViewController
@property GettingHereContent *gettingHere;
在SubNavViewController.m
中#import "SubNavViewController.h"
#import "GettingHereViewController.h"
#import "GettingHereContent.h"
@interface SubNavViewController ()
@end
@implementation SubNavViewController
@synthesize gettingHere;
以下是我创建GettingHere对象的方法: GettingHere.h
#import <Foundation/Foundation.h>
@interface GettingHereContent : NSObject
@property (nonatomic, strong) NSString *content;
@end
GettingHere.m
#import "GettingHereContent.h"
@implementation GettingHereContent
@synthesize content;
@end
答案 0 :(得分:0)
你永远不会为你的gettingHere属性分配init。在你的VC的init方法中试试这个
gettingHere = [[GettingHereContent alloc] init];
另外不要忘记发布它:从这里回答:alloc + init with synthesized property - does it cause retain count to increase by two? @interface Foo:Bar { SomeClass * bla; } @property(非原子,保留)SomeClass * bla; @end
@implementation Foo @synthesize bla; - (id)init { ... bla = [[SomeClass alloc] init]; ... } - (void)dealloc { [bla release]; ... [super dealloc]; }