无法在我的其他视图控制器中访问我的阵列

时间:2012-08-08 19:21:53

标签: objective-c ios xcode nsarray

我在我的CRHViewControllerScript类中创建了一个名为“bulletedArray”的NSArray,我试图访问同一个数组并将其写入我的CRHCommentSection类的NSLog中。但是,它不是在编写数组。我知道数组工作,可以在第一堂课中写出来。我认为我进入下一堂课的方法很糟糕,但这是我的代码的样子。

“CRHViewControllerScript.h”

@interface CRHViewControllerScript : UIViewController <UITextViewDelegate> {

__weak IBOutlet UITextView *myTextView;

}

+ (NSArray*)theArray;
- (IBAction)chalkYes:(id)sender;
@end

“CRHViewControllerScript.m”

#import "CRHViewControllerScript.h"

static NSArray* bulletedArray = nil;

@interface CRHViewControllerScript ()

@end

@implementation CRHViewControllerScript

+ (NSArray*)theArray {
return bulletedArray;
}

- (IBAction)chalkYes:(id)sender {

//get the text inside the textView
NSString *textContents = myTextView.text;

textContents = [textContents stringByReplacingOccurrencesOfString:@"\n" withString:@""];

//make the array
NSArray *bulletedArray = [textContents componentsSeparatedByString:@"\u2022"];

//eliminate blank component at top of the array ("")
NSUInteger len = bulletedArray.count;
if (bulletedArray.count) {

    bulletedArray = [bulletedArray subarrayWithRange:NSMakeRange(1, len-1)]; 
}

//print out array 
NSLog(@"%@",bulletedArray); 

}

CRHCommentSection.h

#import <UIKit/UIKit.h>

@interface CRHCommentSection : UIViewController




@end

CRHCommentSection.m

#import "CRHCommentSection.h"
#import "CRHViewControllerScript.h"

@interface CRHCommentSection ()

@end

@implementation CRHCommentSection

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    NSArray *myArray = [CRHViewControllerScript theArray]; // This is how to call a Class     Method in Objective C
    NSLog(@"%@", myArray); 
}

3 个答案:

答案 0 :(得分:1)

//make the array
NSArray *bulletedArray = [textContents componentsSeparatedByString:@"\u2022"];

这将创建一个名为bulletedArray的新变量,该变量位于

范围内
-(IBAction)chalkYes:(id)sender {

因此它掩盖了你在外面声明的全局声明。只需从头开始删除NSArray*即可解决问题:

bulletedArray = [textContents componentsSeparatedByString:@"\u2022"];

编辑:

实例级方法和类级方法的命名法是分别以-+开头的方法之间的区别。

在您的代码中,+(NSArray*)theArray;是一个类级方法,可以使用类名作为方法的接收者来调用[CRHViewControllerScript theArray];

-(IBAction)chalkYes:(id)sender;是一个实例级方法,必须在类的实际实例上调用,例如[myCRHViewScript chalkYes:nil];

正如@ user490696指出的那样,理想情况下,您应该将数组存储在实例级属性中,并从方法返回,而不是将其存储在全局范围内。例如,您的myTextView变量是ivar或实例变量。这是一个存储每个实例的变量。您可以创建一个返回此变量的方法,如下所示:

-(UITextView*) getTextView
{
    return myTextView;
}

答案 1 :(得分:0)

NSArray *bulletedArray = [textContents componentsSeparatedByString:@"\u2022"];

//eliminate blank component at top of the array ("")
NSUInteger len = bulletedArray.count;
if (bulletedArray.count) {

    bulletedArray = [bulletedArray subarrayWithRange:NSMakeRange(1, len-1)]; 
}

以上代码对您声明的静态没有影响。本地声明

NSArray *bulletedArray //hides the static variable

更改局部变量的名称,然后分配回静态(如果这是您的意图)。

答案 2 :(得分:0)

您需要从不是来自该类的实例调用它。

只需向CRHCommentSection添加一个属性:

@property(strong, nonatomic) CRHViewControllerScript *theControllerScript

并在您的应用中设置将CRHViewControllerScript的实例委托给CRHCommentSection实例,如下所示

theCRHCommentSection.theControllerScript = theViewCRHViewControllerScript; //replace those Values with the actual ones

您可以像CRHCommentSection这样访问它:

[self.theControllerScript theArray];
相关问题