1

In the iOS application, I need to pass an object from one view controller to another and then switch to the new view controller. The goal is to simply relay the information. However, the object becomes NULL and is devoid of any information when the next view showed up. what? could be the reason?

Below is part of my code.

#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 is my custom subclass to NSObject, which has some NSStrings, and it's not nil, but when I pass it onto the next view controller, it becomes NULL.

4

5 回答 5

2

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

我觉得这是ARC的一个缺陷,觉得你的ARCsubjectController可能没用,所以就放出来了subjectController。将您的subjectController作为属性,因此当您将其传递给另一个视图控制器时,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];
}
于 2013-04-25T08:43:10.720 回答
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];
于 2013-04-25T10:04:37.097 回答
0

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

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

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

 -(id)copyWithZone:(NSZone*) zone
于 2013-04-25T09:32:15.013 回答
0

利用

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

实现委托 BIDCourse

 -(id)copyWithZone:(NSZone*) zone
于 2013-04-25T10:28:16.857 回答
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;
}
于 2013-04-25T08:44:08.523 回答