-1

hi I’m trying load data from my online server to view controller but I m getting few issues in that. Using the nsobject and json I’m trying to load in my view controller. I’m stuck with connecting the nsobject to the labels which i have in my storybord.

this is the nsboject code h file and m file

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

this is code which i have used in my viewcontorller m file to get the 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 stuck with the connecting with mutable array to the label which in my view controller so pls somebody suggest weather this is right way to do or what i have to do connect the datas to my view controller..

in tableview we use like we use like this:

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

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

i want to do same thing for my controller view

thanks

kumar
  • 117
  • 3
  • 13
  • Are you using a storyboard in which the view controller is created with the labels? If so, is the Custom Class for the view controller set to `pollingpoliticalViewController` and the label Outlets set? – bobnoble Feb 15 '14 at 11:27
  • s the oultllets everthing is fine @bobnoble – kumar Feb 15 '14 at 11:45

1 Answers1

0

You should have to use NSURLConnection class for loading data from server. See example from following link:- http://codewithchris.com/tutorial-how-to-use-ios-nsurlconnection-by-example/#asynchronous

Also there is an external library for networking called AFNetworking, which is one of the best networking library for iOS and Mac OS X. You can visit this link:- http://afnetworking.com/ . You can find tutorial on AFNetworking from following link:- http://www.raywenderlich.com/30445/afnetworking-crash-course

If you want to go with synchronous method then you should have to use dispatch_async like as

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

Hope It will help you.

Sunil Zalavadiya
  • 1,993
  • 25
  • 36
  • hi sunil im trying load NString not image or video the link u give it will useful for loading image and video not for string and im view using the single view controller pls tell me how to do it – kumar Feb 15 '14 at 11:07
  • @user3247287 As per Apple document says, Do not use this synchronous method to request network-based URLs. For network-based URLs, this method can block the current thread for tens of seconds on a slow network, resulting in a poor user experience, and in iOS, may cause your app to be terminated. Check this url:- https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/NSData_Class/Reference/Reference.html#//apple_ref/occ/clm/NSData/dataWithContentsOfURL: – Sunil Zalavadiya Feb 15 '14 at 11:21
  • @user3247287 You can get ideas about synchronous & asynchronous downloading method from following links:- 1) http://technet.weblineindia.com/mobile/download-files-from-server-in-ios-app-using-synchronous-and-asynchronous-methods/ and 2) http://codewithchris.com/tutorial-how-to-use-ios-nsurlconnection-by-example/#synchronous – Sunil Zalavadiya Feb 15 '14 at 11:23
  • everything is ok sunil but how can i connect the pollarray to the label which im using in my storybord ?... – kumar Feb 15 '14 at 11:37
  • @user3247287 So I think you are new to iOS development, If you want to assign data from pollarray then you have to drag the Label to view in storyboard and need to link it with variable by using IBOutlet. You can learn from following link:- http://www.raywenderlich.com/25561/learn-to-code-ios-apps-3-your-first-app – Sunil Zalavadiya Feb 15 '14 at 11:44
  • @user3247287 If you are not familiar with data types like NSArrat, NSDictionary,etc, then visit following link:- http://rypress.com/tutorials/objective-c/data-types.html – Sunil Zalavadiya Feb 15 '14 at 11:46
  • i have already connected to the h file and i have synthesized in my m file also im asking how to connect that array object to the labels – kumar Feb 15 '14 at 11:49
  • fieldpolitical * cunt=[eventarray objectAtIndex:indexPath.row]; detailvc.detail = cunt.title; detailvc.pla = cunt.place; detailvc.tim = cunt.time; detailvc.dat = cunt.date; // detailvc.stott – kumar Feb 15 '14 at 11:50
  • @user3247287 Hmmm, here what is fieldpolitical * cunt? – Sunil Zalavadiya Feb 15 '14 at 11:51
  • thats my nsobject which have used in my priouvious project in tableview – kumar Feb 15 '14 at 11:52
  • @user3247287 Are you able to NSLog value from that NSObject? – Sunil Zalavadiya Feb 15 '14 at 11:53
  • @user3247287 If 'detail' is UILabel variable in 'detailvc.detail' then you have to use detailvc.detail.text = cunt.title; instead of detailvc.detail = cunt.title; – Sunil Zalavadiya Feb 15 '14 at 11:54
  • @user3247287 Let me know what happen. If its right then I will update my answer. – Sunil Zalavadiya Feb 15 '14 at 11:55
  • i have tried but not able to do @sunil if u dont mind can have ur gmail id or skype id so we can able to discuss clearly – kumar Feb 15 '14 at 11:58
  • @user3247287 Currently I will not available after few time, so can't give email address. But my question is here what are detailvc.detail, detailvc.pla, detailvc.tim, etc? These are UILabel object? – Sunil Zalavadiya Feb 15 '14 at 12:01
  • thats entire code which i have used to in my privous project using tableview and detailview ddtailvc is my detailview value – kumar Feb 15 '14 at 12:06
  • @user3247287 Yes that i got. Now what is detail, pla, tim, etc? – Sunil Zalavadiya Feb 15 '14 at 12:07