2013-04-25 56 views
1

在iOS应用程序中,我需要将对象从一个视图控制器传递到另一个视图控制器,然后切换到新的视图控制器。目标是简单地传递信息。但是,该对象变成NULL,并且在下一个视图出现时没有任何信息。什么?可能是原因?传递给另一个视图控制器时对象变为空

下面是我的代码的一部分。

#pragma mark - Table view delegate 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    BIDCourse *course = self.courses[indexPath.row]; 


    NSString *sub = course.subject; 

    BIDSubjectMainTwoViewController *subjectController=[[BIDSubjectMainTwoViewController alloc] init]; 

    subjectController.title = sub; 


    subjectController.course=course; 

    [self.navigationController pushViewController:subjectController animated:YES]; 
} 

BIDCourse *course是我的自定义子类NSObject,其中有一些NSStrings,它不是零,但是当我将它传递到下一个视图控制器,它成为NULL

+1

您正在使用什么样的内存管理实行委托?弧?你的课程设置如何?课程对象最有可能在使用之前被解除分配。请添加更多代码,以使其可见。 – Kobski 2013-04-25 08:40:08

+0

合成对象过程。 – Balu 2013-04-25 08:41:23

+0

你在代码中的某个地方分配吗?我也有同样的问题,只是你必须先分配它! – 2013-04-25 09:16:51

回答

1

我遇到了像你这样的问题,当我将一个对象传递给另一个viewController时,它已经被释放。

我觉得这是ARC的一个缺陷,ARC认为你的subjectController可能没用,所以它释放了subjectController。将你的subjectController作为一个属性,所以当你将它传递给另一个viewController时ARC不会释放它。

如:

#pragma mark - Table view delegate 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
BIDCourse *course = self.courses[indexPath.row]; 


NSString *sub = course.subject; 

_subjectController=[[BIDSubjectMainTwoViewController alloc] init]; 

_subjectController.title = sub; 


_subjectController.course=course; 

[self.navigationController pushViewController:_subjectController animated:YES]; 
} 
+0

它看起来好像这也适用于我的情况,但它不起作用。奇怪的是,我的NSLog在新视图控制器中检查“课程”之前,已经加入了上述代理方法,比如2013-04-25 05:12:07.681 Marks [71737:c07] course :(空) 2013-04-25 05:12:07.684 Marks [71737:c07] course:subject:英语numQ:10 Quizworth:1 numTest:5 testworth:7 midworth:25 finalworth:30 – 2013-04-25 09:16:05

0

莫非问题出在哪里在BIDSubjectMainTwoViewController.m?

subjectController.title = sub; 
subjectController.course = course; 

在您的BIDSubjectMainTwoViewController.m文件中,您是否在@synthesize您的属性?

@synthesize title = _title; 
@synthesize course = _course; 

您还可以通过变量以及INIT功能ALA:

BIDSubjectMainTwoViewController.h

- (id)initWithTitle:(NSString *)title 
      andCourse:(BIDCourse *)course; 

BIDSubjectMainTwoViewController.m

- (id)initWithTitle:(NSString *)title 
      andCourse:(BIDCourse *)course 
{ 
    self = [super init]; 
    if (self) { 
     // Custom initialization 
     _title=title; 
     _course = course; 
    } 
    return self; 
} 
+0

谢谢,但是当我尝试使用initWithTitle。 。方法,问题仍然存在 – 2013-04-25 09:04:08

+1

Xcode自动综合ivars的属性 – Kreiri 2013-04-25 10:16:08

0

我遇到过这种类似的行为。我的解决方案是制作对象的副本。为您的情况:

BIDCourse *course = [self.courses[indexPath.row] copy]; 

并确保您在BIDCourse .m fike中实现copyWithZone。

-(id)copyWithZone:(NSZone*) zone 
+0

为什么不会强大的工作? – Mar0ux 2013-04-26 12:39:23

0

试试这个:

BIDSubjectMainTwoViewController.h

NSString *strTemp; 

    @property(nonatomic,retain) NSString *strTemp; 

    -(void) setValuestrTemp : (NSString *) str; 

.m文件

@synthesize strTemp; 

//写setter方法

-(void) setValuestrTemp : (NSString *) str { 
     self.strTemp = str; 
    } 

并在viewWillAppear中使用strTemp。

//现在使用它

BIDSubjectMainTwoViewController *subjectController=[[BIDSubjectMainTwoViewController alloc] init]; 

     [subjectController setValuestrTemp:sub]; 

    [self.navigationController pushViewController:_subjectController animated:YES]; 
0

使用

NSString *sub = [course.subject copy]; 
    subjectController.course=[course copy]; 

BIDCourse

-(id)copyWithZone:(NSZone*) zone 
相关问题