我有两个视图控制器,一个传递对象,另一个按照预期传递给另一个。但是当我尝试传递字符串时,某种程度上会发生一些事情,因为在另一个ViewController上它始终返回为(null)。我在这里遗失了什么?
ViewController one.h
@class ViewControllerTwo;
@interface ViewControllerOne : UIViewController{
ViewControllerTwo* playboard;
@property(nonatomic, retain) ViewControllerTwo* playBoard;
@end
ViewController One
playBoard = [[ViewControllerTwo alloc] init];
[playBoard setQuickPlayFilters:[[NSMutableArray alloc] init]];
[playboard setChallenge:[[NSString alloc] init]];
passToPlayboard = [[NSString alloc] initWithString:@"one"];
filters = [[NSMutableArray alloc] initWithObjects:
[NSNumber numberWithInt:5],
[NSNumber numberWithInt:1],
[NSNumber numberWithInt:1],
[NSNumber numberWithInt:2],
[NSNumber numberWithInt:9],
[NSNumber numberWithInt:0],
[NSNumber numberWithInt:0],
[NSNumber numberWithInt:8], nil];
[playBoard setQuickPlayFilters:[filters mutableCopy]];
[playboard setChallenge:[NSString stringWithString:passToPlayboard]];
Touch_TestingAppDelegate* appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate.navController pushViewController:playBoard animated:YES];
ViewController Two.h
#import <UIKit/UIKit.h>
@interface ViewControllerTwo : UIViewController {
NSString* challenge;
NSMutableArray* quickPlayFilters;
}
@property(nonatomic, retain) NSString* challenge;
@property(nonatomic, retain) NSMutableArray* quickPlayFilters;
@end
ViewController Two.m
- (void)viewDidLoad{
[super viewDidLoad];
cover = [[CoverView alloc] initWithFrame:CGRectMake(140.0f, 80.0f, 620.0f, 610.0f) andRemaningTiles:[quickPlayFilters lastObject]];
cover.backgroundColor = [UIColor clearColor];
[self.view addSubview:cover];
NSLog(@"Quick Play : %@", quickPlayFilters);
NSLog(@"Challenge from Load : %@", challenge);
[[self cover] setFilters:[quickPlayFilters mutableCopy]];
timerDisplay = [[TimerDisplay alloc]
initWithFrame:CGRectMake(813.f, 120.f, 181.f, 162.f)
andTimeCycle:[quickPlayFilters objectAtIndex:0]
andNumberOfRows:[quickPlayFilters objectAtIndex:6]];
}
当我在quickPlayFilters
中寻找playBoard
时,我可以立即找到它们。但是我不能为challenge
字符串返回除null之外的任何内容。请帮忙。
答案 0 :(得分:3)
主要问题是ViewControllerTwo.h
中的拼写错误。 playboard
iVar全部为小写,但属性为camelcase(playBoard
)。这导致您的班级有两个iVars(playboard
和playBoard
)。修复拼写错误,以便所有内容都为playBoard
,并且可以正常运行。
我的初步答案留待参考,因为我仍然建议您按照我的建议清理代码。我修改了代码以在所有实例中使用playBoard
。
=原始答案
如果没有在ViewControllerTwo.m
中查看您的代码,则无法回答这个问题。确保challenge
的属性定义为:
@property (nonatomic, copy) NSString * challenge;
如果您有自定义设置器,请确保它正在执行正确的分配ala:
- (void)setChallenge:(NSString *)newChallenge
{
if (![challenge isEqualToString:newChallenge]) {
challenge = [newChallenge copy];
// whatever else you need to do
}
}
你正在创建一堆你并不真正需要的对象。你上面的代码可能是:
playBoard = [[ViewControllerOne alloc] init];
passToPlayBoard = [[NSString alloc] initWithString:@"one"];
filters = [[NSMutableArray alloc] initWithObjects:
[NSNumber numberWithInt:5],
[NSNumber numberWithInt:1],
[NSNumber numberWithInt:1],
[NSNumber numberWithInt:2],
[NSNumber numberWithInt:9],
[NSNumber numberWithInt:0],
[NSNumber numberWithInt:0],
[NSNumber numberWithInt:8], nil];
[playBoard setQuickPlayFilters:filters];
[playBoard setChallenge:passToPlayBoard];
Touch_TestingAppDelegate* appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate.navController pushViewController:playBoard animated:YES];
注意:上面的代码假设您的项目使用ARC。如果没有,您将必须适当地释放对象。
编辑:发现实际问题。添加到这个答案的顶部。答案 1 :(得分:0)
在setChallenge方法中,请保留您的对象值.. like。
(void)setChallenge:(NSString *)newChallenge {
challenge = [newChallenge retain];
}