简体   繁体   中英

Call return method through view controllers

I have two different view controllers ViewController.m & teacherList.m. I am trying to call the -(NSMutableArray *)getTeachersList method from teacherList.m to have it create an array for me and then be able to use it in ViewController.m. I want to do this just hard coding it and not with sequencing or use of storyboard

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 File

#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

Don't put the method getTeachersList in a view controller. Create a singleton object that is a subclass of NSObject . Let's call it TeacherListManager .

Then ask the singleton for the teacher's list.

The call might look like this:

TeacherListManager *theTeacherListManager = [TeacherListManager sharedTeacherListManager];

NSMutableArray *theTeacherList = theTeacherListManager.teachersList;

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

Do a search on Objective C singleton design pattern for more information.

Your getTeachersList method is currently written to get the teachers list using synchronous networking calls, which is bad. If anything slows down the network connection then it can hang the UI for up to 2 minutes until the connection times out.

you should rewrite that method to download the data asynchronously.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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