2013-10-22 82 views
1

我有兩個不同的視圖控制器ViewController.m & teacherList.m。我想從teacherList.m中調用 - (NSMutableArray *)getTeachersList方法讓它爲我創建一個數組,然後能夠在ViewController.m中使用它。我想這樣做,只是硬編碼它,而不是用測序或使用故事板通過視圖控制器調用返回方法

ViewController.m的

#import "ViewController.h" 
#import "teacherList.h" 

@interface ViewController() 

@end 

@implementation ViewController 
@synthesize userView; 
- (void)viewDidLoad 
{ 
NSMutableArray *hey = [teacherList getTeachersList];; 

NSString *hello = [hey objectAtIndex:0]; 

[self say:hello]; 

[super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 
} 

- (void)say:(NSString *)greet{ 
userView.text = greet; 
} 


- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 

@end 

teacherList.m文件

#import "teacherList.h" 

@interface teacherList() 

@end 

@implementation teacherList 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
if (self) { 
    // Custom initialization 
} 
return self; 
} 

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

- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 

-(NSMutableArray *)getTeachersList{ 
NSURL *website = [NSURL URLWithString:@"http://www2.qcc.mass.edu/facAbsence/default.asp"]; 



NSURLRequest *request = [NSURLRequest requestWithURL:website]; 



NSURLResponse* response = nil; 

NSError *error = nil; 

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

//storing data in a string 

NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 

//putting it into an array to be 

//able to working wiht string or array 

NSArray *newString = [myString componentsSeparatedByString:@"\n"]; 


NSMutableArray *teacherArray = [[NSMutableArray alloc]init]; 
NSMutableString *curLineNum; 
NSString *curLine; 

int i; 

for (i = 0; i <[newString count]; i++) { 

    curLine = [newString objectAtIndex:i]; 

    if ([curLine rangeOfString:@"<strong>"].location != NSNotFound) { 

     NSScanner *theScanner = [NSScanner scannerWithString:curLine]; 

     [theScanner scanUpToString:@"<strong>" intoString:NULL]; 

     [theScanner setScanLocation: [theScanner scanLocation]+8]; 

     [theScanner scanUpToString:@"</strong>" intoString:&curLineNum]; 

     [teacherArray addObject:curLineNum]; 

    } 

} 
return teacherArray; 

} 


@end 
+2

你現有的代碼有什麼問題? – Marc

+0

您的代碼按原樣需要定義teacherList的實例。您可以改爲使用getTeachersList方法代替類方法。 – rocky

+0

回答你現在已經刪除的問題,找到一個iOS庫,以編程方式使用cookie支持HTTP瀏覽。您需要在POST操作中提交憑據,而不是在URL中。 – halfer

回答

1

不要把方法getTeachersList在一個視圖控制器。創建一個單獨的對象,它是NSObject的一個子類。我們稱之爲TeacherListManager

然後問單身老師的名單。

呼叫可能是這樣的:

TeacherListManager *theTeacherListManager = [TeacherListManager sharedTeacherListManager]; 

NSMutableArray *theTeacherList = theTeacherListManager.teachersList; 

if (theTeacherList == nil) 
    //Handle the case where the teacher list hasn't been loaded yet. 

請在目標C singleton設計模式搜索以獲取更多信息。

您的getTeachersList方法當前編寫爲使用同步聯網調用來獲取教師列表,這很糟糕。如果有任何事情減緩網絡連接,那麼它可以掛起用戶界面長達2分鐘,直到連接超時。

您應該重寫該方法以異步下載數據。

相關問題