[英]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.