gpt4 book ai didi

iphone - 通过 View Controller 调用返回方法

转载 作者:行者123 更新时间:2023-11-29 13:02:07 24 4
gpt4 key购买 nike

我有两个不同的 View Controller ViewController.m 和 teacherList.m。我试图从 teacherList.m 调用 -(NSMutableArray *)getTeachersList 方法让它为我创建一个数组,然后能够在 ViewController.m 中使用它。我想这样做只是硬编码,而不是排序或使用 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文件

#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

最佳答案

不要将方法 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 方法当前编写为使用同步网络调用获取教师列表,这是不好的。如果有任何东西减慢了网络连接速度,那么它可以挂起 UI 长达 2 分钟,直到连接超时。

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

关于iphone - 通过 View Controller 调用返回方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19524484/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com