繁体   English   中英

创建一个将textlabel.text设置为index(x)处的nsarray的方法,而x

[英]create a method that sets textlabel.text to nsarray at index(x) while x<nsarray.count objective c

我已经用一个文本文件创建了一个NSArray

void arrayfromfile {

// get a reference to our file
NSString *myPath = [[NSBundle mainBundle]pathForResource:@"CalendarQuotes" ofType:@"txt"];

// read the contents into a string
NSString *myFile = [[NSString alloc]initWithContentsOfFile:myPath encoding:NSUTF8StringEncoding error:nil];

// split our string into an array
NSArray *mySplit = [myFile componentsSeparatedByString:@"\n"];}

现在,我要做的是将label.text设置为mySplit[x] while x< mysplit.count并在每次用户向上滑动时调用它。

setlabeltextmethod quote {
if x<mySplit.count {
label.text = mySplit objectatindex x
x+=1
else { label.text = @"thats it folks" }
} 

我遇到的麻烦是从报价单访问mySplit ,因此我可以对其进行操作以在索引x处给我报价。

当我尝试通过滑动方法访问它时:

- (IBAction)swipeUp:(id)sender {

[self doAnimationUp];
NSLog (@"swipedUp");
NSLog(@"array%@" , [mySplit objectAtIndex:0]);
NSLog(@"arraycreated");
//I'm using NSLog for debug purposes but this is where I would setlabeltext.text to mySplit objectAtIndex
}

我得到array[null]

mySplit变量是本地arrayfromfile ,所以一旦该方法返回变量已经一去不复返了。 解决此问题的方法不止一种。 一种是使它成为实例变量,即属于您当前类实例并且对每个实例方法可见的变量。 像这样开始执行:

@implementation MyClassName
{
    NSArray *mySplit;
}

现在设置的实例方法变量arrayfromfile和实例方法读它swipeUp -这两个调用必须在你的类的同一实例进行,每个实例都有自己的一套实例变量。

其他选项包括将属性添加到类,或者(如果必须)添加某种全局变量。

高温超导

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM