通过视图控制器调用return方法

时间:2013-10-22 17:25:33

标签: iphone ios objective-c uiviewcontroller

我有两个不同的视图控制器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

1 个答案:

答案 0 :(得分: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.

搜索Objective C单例设计模式以获取更多信息。

目前编写getTeachersList方法是为了使用同步网络呼叫获取教师列表,这很糟糕。如果网络连接速度变慢,那么它可以将UI挂起最多2分钟,直到连接超时。

您应该重写该方法以异步下载数据。