2014-02-15 60 views
-1

嗨我想從我的在線服務器加載數據到視圖控制器,但我得到的問題很少。使用nsobject和json我試圖加載我的視圖控制器。我堅持將nsobject連接到我在故事曲目中的標籤。如何從視圖控制器中的服務器加載數據IOS

這是nsboject代碼.h文件和M文件

@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; 

這是我在viewcontorller M檔都用於獲取DATAS代碼。

#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]; 
    } 
    } 

IM卡與可變數組的標籤在我看來控制器,請有人建議天氣的連接,這是正確的方式做或我必須做的DATAS連接到我的視圖控制器的..

在tableview中,我們使用像我們這樣使用:

fieldpolitical * cunt=[eventarray objectAtIndex:indexPath.row]; 

    detailvc.detail = cunt.title; 
    detailvc.pla = cunt.place; 
    detailvc.tim = cunt.time; 
    detailvc.dat = cunt.date; 
    // detailvc.stott 

我想要做同樣的事情對我的控制程序圖

感謝

+0

您是否使用了一個使用標籤創建視圖控制器的故事板?如果是這樣,視圖控制器的自定義類是否設置爲「pollingpoliticalViewController」,標籤Outlets是否設置? – bobnoble

+0

s oultllets everthing很好@bobnoble – kumar

回答

0

您應該使用NSURLConnection類從服務器加載數據。從下面的鏈接見例如: - http://codewithchris.com/tutorial-how-to-use-ios-nsurlconnection-by-example/#asynchronous

也有聯網被稱爲AFNetworking,這是最好的網絡庫的iOS和Mac OS X你可以訪問此鏈接的一個外部庫: - http://afnetworking.com/。 您可以從以下鏈接AFNetworking找到教程: - http://www.raywenderlich.com/30445/afnetworking-crash-course

如果你想與同步方法去,那麼你應該使用dispatch_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]; 
     }); 
    }); 

希望它會幫助你。

+0

嗨sunil im試圖加載NString不是圖像或視頻鏈接ü給它將有用的加載圖像和視頻不是字符串和即時查看使用單個視圖控制器請告訴我如何做到這一點 – kumar

+0

@ user3247287按照Apple文檔說,不要使用這種同步方法來請求基於網絡的URL。對於基於網絡的URL,此方法可以在慢速網絡上阻止當前線程幾十秒,從而導致糟糕的用戶體驗,並且在iOS中可能會導致您的應用被終止。檢查此網址: - https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/NSData_Class/Reference/Reference.html#//apple_ref/occ/clm/NSData/dataWithContentsOfURL: –

+0

@ user3247287您可以從以下鏈接獲得有關同步和異步下載方法的建議: - 1)http://technet.weblineindia.com/mobile/download-files-from-server-in-ios-app-using-synchronous-and -asynchronous-methods /和2)http://codewithchris.com/tutorial-how-to-use-ios-nsurlconnection-by-example/#synchronous –

相關問題