无法在我的其他视图控制器中访问我的阵列
我在我的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);
}
//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;
}
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
更改局部变量的名称,然后分配回静态的,如果这是你的意图。
您需要从不是来自该类的实例调用它。
只是一个属性添加到CRHCommentSection
:
@property(strong, nonatomic) CRHViewControllerScript *theControllerScript
,并在应用程序委托的CRHViewControllerScript
的情况下设置为CRHCommentSection
实例这样
theCRHCommentSection.theControllerScript = theViewCRHViewControllerScript; //replace those Values with the actual ones
你可以接取它CRHCommentSection
这样:
[self.theControllerScript theArray];
另外,我wouldnt假设在静态数据中传递数据是理想的,最好使用实例方法或属性,并获得对所需视图控制器实际实例的引用。 – 2012-08-08 19:28:59
@ user490696是的,作为一个规则,应该避免使用静态全局变量,但是我只是特别指出了问题的根源,这就是范围问题 – 2012-08-08 19:30:35
你能给我一个实例方法的例子吗?我是菜鸟。 – Craig 2012-08-08 19:31:40