1

ViewController.m と teacherList.m という 2 つの異なるビュー コントローラがあります。-(NSMutableArray *)getTeachersList メソッドを teacherList.m から呼び出して、配列を作成し、それを 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
4

1 に答える 1

1

getTeachersListメソッドをView Controllerに配置しないでください。のサブクラスであるシングルトン オブジェクトを作成します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.

詳細については、Objective C シングルトン デザイン パターンを検索してください。

あなたのgetTeachersListメソッドは現在、同期ネットワーク呼び出しを使用して教師リストを取得するように書かれていますが、これは悪いことです。ネットワーク接続が遅くなると、接続がタイムアウトするまで最大 2 分間 UI がハングアップする可能性があります。

そのメソッドを書き直して、データを非同期でダウンロードする必要があります。

于 2013-10-22T18:14:35.240 に答える