-1

我正在尝试使用 self 作为指向视图控制器的指针将 self 分配给 antoher 类(nsobject 类)中的属性,但该属性始终为零。你们中的任何人都知道为什么或如何解决?

视图控制器.m

-(void)startToDoSomething:(NSString*)testToRun
{
    SecondClass *secondClass = [[SecondClass alloc] init];
    secondClass.viewController = self;
    [secondClass doSomething];
}

第二类.h:

NSObject 类:

.h 文件

#import "ViewController.h"

@class ViewController;

@interface SecondClass : NSObject
{
    ViewController *viewController;
}

@property (nonatomic,retain) ViewController *viewController;
4

1 回答 1

2

为什么不添加一个 init 方法:

-(id)initWithViewController:(ViewController *)aViewController
{
     self = [super init];

     if(self)
     {
          self.viewController = aViewController;
     }

     return self;
}

然后你可以这样称呼它

SecondClass *secondClass = [[SecondClass alloc] initWithViewController:self];

您还可以将属性(nonatomic, assign)更改为并将合成更改为@synthesize viewController = _viewController

在 SecondClass 中,您使用 _viewController。

希望能帮助到你。

于 2013-08-19T20:39:16.973 回答