如何在viewcontroller IOS中从服务器加载数据
嗨,我正在尝试从我的在线服务器加载数据到视图控制器,但我在这方面遇到了一些问题。使用我试图在视图控制器中加载的nsobject和json。我一直坚持将nsobject连接到storybord中的标签 这是nsboject代码h文件和m文件如何在viewcontroller IOS中从服务器加载数据,ios,objective-c,nsobject,Ios,Objective C,Nsobject,嗨,我正在尝试从我的在线服务器加载数据到视图控制器,但我在这方面遇到了一些问题。使用我试图在视图控制器中加载的nsobject和json。我一直坚持将nsobject连接到storybord中的标签 这是nsboject代码h文件和m文件 @interface vote : NSObject @property (nonatomic,strong) NSString * question; @property (nonatomic,strong) NSString * choose1; @pr
@interface vote : NSObject
@property (nonatomic,strong) NSString * question;
@property (nonatomic,strong) NSString * choose1;
@property (nonatomic,strong) NSString * choose2;
-(id) initWithquestion: (NSString *) qut andchoose1: (NSString *) ch andchoose2: (NSString *) cho;
这是我在VIEWCONTORLER m文件中用来获取数据的代码
#define getDataURL @"http://localhost/poll/view.php"
@implementation pollingpoliticalViewController
@synthesize que,cho1,cho2;
@synthesize json,pollarray;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//vote *cnnt = [pollarray ];
// Do any additional setup after loading the view.
}
-(void) retrieveData
{
NSURL * url =[NSURL URLWithString:getDataURL];
NSData * data=[NSData dataWithContentsOfURL:url];
json =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
pollarray = [[NSMutableArray alloc]init];
for (int i=0; i<json.count; i++) {
NSString * dd =[[json objectAtIndex:i]objectForKey:@"ques"];
NSString * plae= [[json objectAtIndex:i]objectForKey:@"choss1"];
NSString * ti =[[json objectAtIndex:i]objectForKey:@"choss2"];
vote *myarray =[[vote alloc]initWithquestion:dd andchoose1:plae andchoose2:ti];
[pollarray addObject:myarray];
}
}
我想对控制器视图执行相同的操作
谢谢您应该使用
NSURLConnection
类从服务器加载数据。请参见以下链接中的示例:-
此外,还有一个名为AFNetworking
的外部网络库,它是iOS和Mac OS X的最佳网络库之一。您可以访问以下链接:-。
您可以从以下链接找到有关AFNetworking的教程:-
如果您想使用同步方法,那么您应该像下面一样使用dispatch\u async
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^{
// ... perform a blocking, synchronous URL retrieval ...
NSURL * url =[NSURL URLWithString:getDataURL];
NSData * data=[NSData dataWithContentsOfURL:url];
// ... and hop back onto the main queue to handle the result
dispatch_async(dispatch_get_main_queue(),
^{
json =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
pollarray = [[NSMutableArray alloc]init];
for (int i=0; i<json.count; i++) {
NSString * dd =[[json objectAtIndex:i]objectForKey:@"ques"];
NSString * plae= [[json objectAtIndex:i]objectForKey:@"choss1"];
NSString * ti =[[json objectAtIndex:i]objectForKey:@"choss2"];
vote *myarray =[[vote alloc]initWithquestion:dd andchoose1:plae andchoose2:ti];
[pollarray addObject:myarray];
});
});
dispatch\u async(dispatch\u get\u global\u queue(dispatch\u queue\u PRIORITY\u DEFAULT,0),
^{
//…执行阻止、同步URL检索。。。
NSURL*url=[NSURL URLWithString:getDataURL];
NSData*data=[NSData dataWithContentsOfURL:url];
//…并跳回到主队列以处理结果
dispatch\u async(dispatch\u get\u main\u queue(),
^{
json=[NSJSONSerialization JSONObjectWithData:数据选项:kNilOptions错误:nil];
pollarray=[[NSMutableArray alloc]init];
对于(int i=0;ihi sunil im尝试加载NString not image或video u给出的链接它将有助于使用单视图控制器加载图像和视频not string和im视图请告诉我如何做it@user3247287正如苹果的文档所说,不要使用这种同步方法来请求基于网络的URL可以在慢速网络上阻止当前线程数十秒,导致用户体验不佳,在iOS中,可能会导致应用程序终止。请检查此url:-:@user3247287您可以从以下链接了解同步和异步下载方法:-1)和2)一切正常,sunil,但是我如何将pollarray连接到我在storybord中使用的标签?..@user3247287所以我认为您是iOS开发的新手,如果您想从pollarray分配数据,那么您必须将标签拖到storyboard中查看,并需要使用IBOutlet将其与变量链接。您可以从以下链接中学习:-您使用的是使用标签创建视图控制器的故事板吗?如果是,视图控制器的自定义类是否设置为pollingpoliticalViewController
,标签出口是否设置为“Oultlet Everything is fine@bobnoble”
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^{
// ... perform a blocking, synchronous URL retrieval ...
NSURL * url =[NSURL URLWithString:getDataURL];
NSData * data=[NSData dataWithContentsOfURL:url];
// ... and hop back onto the main queue to handle the result
dispatch_async(dispatch_get_main_queue(),
^{
json =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
pollarray = [[NSMutableArray alloc]init];
for (int i=0; i<json.count; i++) {
NSString * dd =[[json objectAtIndex:i]objectForKey:@"ques"];
NSString * plae= [[json objectAtIndex:i]objectForKey:@"choss1"];
NSString * ti =[[json objectAtIndex:i]objectForKey:@"choss2"];
vote *myarray =[[vote alloc]initWithquestion:dd andchoose1:plae andchoose2:ti];
[pollarray addObject:myarray];
});
});