Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 17.28 KB  |  hits: 29  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to populate UITableView with the responce of JSON from a different ViewController?
  2. #import <UIKit/UIKit.h>
  3.  
  4. @interface BNT_1DetailViewController : UIViewController <UISplitViewControllerDelegate>
  5. {
  6.     NSMutableArray *buttons;
  7.     NSString *userName, *password, *serverIP;
  8.     NSDictionary *mess;
  9.     IBOutlet UITextField *userNameF;
  10.     IBOutlet UITextField *passwordF;
  11.     IBOutlet UITextField *serverF;
  12.     NSURL *url;
  13.     NSMutableArray *mesag;
  14.     IBOutlet UIButton *acceptButton;
  15.     NSMutableArray *mesID; //saving meesage ID s to NSMutableArray
  16.     NSMutableArray *content;
  17.  
  18. }
  19. @property(retain,nonatomic)NSMutableArray *mesID,*content;
  20. @property(assign,nonatomic)NSMutableArray *mesag ;
  21. @property(retain, nonatomic)NSDictionary *mess;
  22. //@property (retain,nonatomic)  NSMutableArray *buttons;
  23. @property (retain, nonatomic) UITextField *userNameF;
  24. @property (retain, nonatomic) UITextField *passwordF;
  25. @property (retain, nonatomic) UITextField *serverF;
  26. @property (retain, nonatomic) UIButton *acceptButton;
  27. @property (retain, nonatomic) NSString *userName, *password, *serverIP;
  28. @property (strong, nonatomic) id detailItem;
  29. @property(retain,nonatomic)NSURL *url;
  30. @property (strong, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
  31.  
  32. -(IBAction)acceptAct:(id)sender;
  33. -(IBAction)readMessages:(id)sender;
  34. -(IBAction)secondAct:(id)sender;
  35. @end
  36.        
  37. #import "BNT_1DetailViewController.h"
  38. #import "mainMenu.h"
  39. #import "messages.h"
  40. @interface BNT_1DetailViewController ()
  41. @property (strong, nonatomic) UIPopoverController *masterPopoverController;
  42. - (void)configureView;
  43. @end
  44.  
  45. @implementation BNT_1DetailViewController
  46. @synthesize content,mesID,url;
  47. @synthesize mesag,mess;
  48. @synthesize userNameF, passwordF, serverF, acceptButton;
  49. @synthesize userName, password, serverIP;
  50. @synthesize detailItem = _detailItem;
  51. @synthesize detailDescriptionLabel = _detailDescriptionLabel;
  52. @synthesize masterPopoverController = _masterPopoverController;
  53.  
  54. -(IBAction)readMessages:(id)sender{
  55.  
  56.     messages *message=[[messages alloc]initWithNibName:@"messages" bundle:nil];
  57.     /*
  58.      message.mesid=mesID;
  59.      message.contents=content;
  60.      */
  61.     [self.navigationController pushViewController:message animated:TRUE];
  62.  
  63.  
  64.  
  65.  
  66. }
  67. -(IBAction)acceptAct:(id)sender{
  68.  
  69.  
  70.     userName=[[NSString alloc] initWithString:userNameF.text ];
  71.     [userNameF setText:userName];
  72.     NSUserDefaults *userNameDef= [NSUserDefaults standardUserDefaults];
  73.     [userNameDef setObject:userName forKey:@"userNameKey"];
  74.     password =[[NSString alloc] initWithString:passwordF.text];
  75.     [passwordF setText:password];
  76.     NSUserDefaults *passDef=[NSUserDefaults standardUserDefaults];
  77.     [passDef setObject:password forKey:@"passwordKey"];
  78.     serverIP=[[NSString alloc] initWithString: serverF.text];
  79.     [serverF setText:serverIP];
  80.     NSUserDefaults *serverDef=[NSUserDefaults standardUserDefaults];
  81.     [serverDef setObject:serverIP forKey:@"serverIPKey"];
  82.     [userNameDef synchronize];
  83.     [serverDef synchronize];
  84.     [passDef synchronize];
  85.  
  86.  
  87.     UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"BNTPRO "
  88.                                                       message:@"Your User Informations are going to be sent to server. Do you accept?"
  89.                                                      delegate:self
  90.                                             cancelButtonTitle:@"OK"
  91.                                             otherButtonTitles:@"Cancel",  nil];
  92.     [message show];
  93.  
  94. }
  95. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  96. {
  97.     NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
  98.  
  99.     if([title isEqualToString:@"OK"])
  100.     {
  101.         if([userNameF.text isEqualToString:@""]|| [passwordF.text isEqualToString:@""] || [serverF.text length]<6)
  102.         {
  103.             UIAlertView *message1 = [[UIAlertView alloc] initWithTitle:@"BNTPRO "
  104.                                                                message:@"Your User Informations are not defined properly!"
  105.                                                               delegate:nil
  106.                                                      cancelButtonTitle:@"OK"
  107.                                                      otherButtonTitles:  nil];
  108.  
  109.             [message1 show];
  110.             [userNameF  resignFirstResponder];
  111.             [passwordF resignFirstResponder];
  112.             return;
  113.         }
  114.         mainMenu *ma=[[mainMenu alloc]initWithNibName:@"mainMenu" bundle:nil];
  115.         NSString *str1=[@"?username=" stringByAppendingString:userNameF.text];
  116.         NSString *str2=[@"&password=" stringByAppendingString:passwordF.text];
  117.         NSString *str3=[str1 stringByAppendingString:str2];
  118.         NSString *str4 =[@"http://" stringByAppendingString:serverF.text];
  119.  
  120.  
  121.        url=[NSURL URLWithString:[str4 stringByAppendingString:[@"/ipad/login.php" stringByAppendingString:str3]]];
  122.         //get the url to jsondata
  123.         NSData *jSonData=[NSData dataWithContentsOfURL:url];
  124.  
  125.         if (jSonData!=nil) {
  126.             NSError *error=nil;
  127.             id result=[NSJSONSerialization JSONObjectWithData:jSonData options:
  128.                        NSJSONReadingMutableContainers error:&error];
  129.             NSLog(@"%@",result);      
  130.  
  131.             if (error==nil) {
  132.             mess=[result objectForKey:@"message"];
  133.                 NSDictionary *messContent=[mess valueForKeyPath:@"message"];
  134.                 NSDictionary *messID=[mess valueForKeyPath:@"ID"];
  135.                 NSString*key1=[ result objectForKey:@"key" ];                
  136.                 NSString *s1=[@"http://" stringByAppendingString:serverF.text];
  137.                 NSString *s2=[s1 stringByAppendingString:@"/ipad/button.php"];
  138.                 NSURL *url2=[NSURL URLWithString:[s2 stringByAppendingString:[@"?key=" stringByAppendingString:key1]]];
  139.  
  140.                 NSData *data2=[NSData dataWithContentsOfURL:url2];
  141.                 id result2=[NSJSONSerialization JSONObjectWithData:data2 options:NSJSONReadingMutableContainers error:nil];
  142.  
  143.                 mesID = [NSMutableArray array];//saving meesage ID s to NSMutableArray
  144.                 content = [NSMutableArray array];    
  145.                 for ( mesag in mess) {
  146.  
  147.                     //i want to populate TableViewCells with the messID and the details of each cell(push in new view controller) will contain 'content' above
  148.  
  149.                   /*
  150.                      [mesID addObject:[mesag objectForKey:@"ID"]];
  151.                      [content addObject:[mesag objectForKey:@"message"]];    
  152.                      */    
  153.                     [[NSUserDefaults standardUserDefaults] setObject:messID forKey:@"message"];
  154.                     [[NSUserDefaults standardUserDefaults] setObject:messContent forKey:@"messContent"];
  155.  
  156.                     // NSLog(@"%@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
  157.                     //   NSLog(@"The Message with ID %@ is: %@n",messID,messContent);
  158.  
  159.                 }
  160.  
  161.                 NSLog(@"MessID: %@",mesID);
  162.                 NSLog(@"Context: %@",content);
  163.  
  164.  
  165.                 buttons = [NSMutableArray array];
  166.                 CGFloat yPosition = 60.0f;
  167.                 CGFloat xPosition = 40.0f;
  168.  
  169.                 const CGFloat buttonHeight = 75.0f;
  170.                 const CGFloat buttonMargin = 50.0f;
  171.                 UIButton *bt=[UIButton buttonWithType:UIButtonTypeRoundedRect];
  172.  
  173.                 [bt setTitle:@"Messages" forState:UIControlStateNormal];
  174.                 [bt addTarget:self action:@selector(readMessages:) forControlEvents:UIControlEventTouchUpInside];
  175.                 bt.titleLabel.font=[UIFont systemFontOfSize:25];
  176.                 bt.frame=CGRectMake(300.0f, 700.0f,180.0f , 70.0f);
  177.                 [ma.view addSubview:bt];
  178.  
  179.                 for(NSDictionary* buttonData in result2) {
  180.                     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  181.                     NSString* buttonTitle = [buttonData objectForKey:@"name"];
  182.                     NSString* buttonID=[buttonData objectForKey:@"ID"];
  183.                     [button setTitle:buttonTitle forState:UIControlStateNormal];
  184.                     [button setTag:[buttonID intValue]];
  185.                     button.titleLabel.font=[UIFont systemFontOfSize:28];
  186.                     button.frame = CGRectMake(xPosition, yPosition, 210.0f, buttonHeight);
  187.                     [button addTarget:self action:@selector(secondAct:) forControlEvents:UIControlEventTouchUpInside];
  188.                     [ma.view addSubview:button];
  189.                     //   [v.buttons addObject:button];
  190.  
  191.                     xPosition = 260.0f;
  192.                     yPosition+= buttonHeight + buttonMargin;
  193.  
  194.                 }
  195.  
  196.  
  197.             [self.navigationController pushViewController:ma animated:TRUE];
  198.  
  199.  
  200. }
  201.         }} }
  202.  
  203.  
  204. -(IBAction)secondAct:(id)sender{
  205.  
  206.     NSLog(@"ABC: %d",[sender tag]);
  207.  
  208. }
  209.  
  210. - (void)dealloc
  211. {
  212.     [_detailItem release];
  213.     [_detailDescriptionLabel release];
  214.     [_masterPopoverController release];
  215.     [super dealloc];
  216. }
  217.  
  218. #pragma mark - Managing the detail item
  219.  
  220. - (void)setDetailItem:(id)newDetailItem
  221. {
  222.     if (_detailItem != newDetailItem) {
  223.         [_detailItem release];
  224.         _detailItem = [newDetailItem retain];
  225.  
  226.         // Update the view.
  227.         [self configureView];
  228.     }
  229.  
  230.     if (self.masterPopoverController != nil) {
  231.         [self.masterPopoverController dismissPopoverAnimated:YES];
  232.     }        
  233. }
  234.  
  235. - (void)configureView
  236. {
  237.     // Update the user interface for the detail item.
  238.  
  239.     if (self.detailItem) {
  240.         self.detailDescriptionLabel.text = [self.detailItem description];
  241.     }
  242. }
  243.  
  244. - (void)didReceiveMemoryWarning
  245. {
  246.     [super didReceiveMemoryWarning];
  247.     // Release any cached data, images, etc that aren't in use.
  248. }
  249.  
  250. #pragma mark - View lifecycle
  251.  
  252. - (void)viewDidLoad
  253. {
  254.     [super viewDidLoad];
  255.     [passwordF setSecureTextEntry:YES];
  256.     userNameF.font=[UIFont systemFontOfSize:24];
  257.  
  258.     userNameF.frame = CGRectMake(320.0f,60.0f,360.0f,50.0f);
  259.     passwordF.frame = CGRectMake(320.0f,145.0f,360.0f,50.0f);
  260.     serverF.frame= CGRectMake(320.0f,235.0f,360.0f,50.0f);
  261.     userNameF.font=[UIFont boldSystemFontOfSize:24];
  262.     [userNameF becomeFirstResponder];
  263.     serverF.font=[UIFont boldSystemFontOfSize:24];
  264.     passwordF.font=[UIFont systemFontOfSize:24];
  265.  
  266.  
  267.     acceptButton.titleLabel.font=[UIFont boldSystemFontOfSize:26];
  268.     // Do any additional setup after loading the view, typically from a nib.
  269.     [self configureView];
  270. }
  271.  
  272. - (void)viewDidUnload
  273. {
  274.     [super viewDidUnload];
  275.     // Release any retained subviews of the main view.
  276.     // e.g. self.myOutlet = nil;
  277. }
  278.  
  279. - (void)viewWillAppear:(BOOL)animated
  280. {
  281.     [super viewWillAppear:animated];
  282. }
  283.  
  284. - (void)viewDidAppear:(BOOL)animated
  285. {
  286.     [super viewDidAppear:animated];
  287. }
  288.  
  289. - (void)viewWillDisappear:(BOOL)animated
  290. {
  291.     [super viewWillDisappear:animated];
  292. }
  293.  
  294. - (void)viewDidDisappear:(BOOL)animated
  295. {
  296.     [super viewDidDisappear:animated];
  297. }
  298.  
  299. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  300. {
  301.     // Return YES for supported orientations
  302.     return YES;
  303. }
  304.  
  305. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  306. {
  307.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  308.     if (self) {
  309.         self.title = NSLocalizedString(@"Detail", @"Detail");
  310.     }
  311.     return self;
  312. }
  313.  
  314. #pragma mark - Split view
  315.  
  316. - (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
  317. {
  318.     barButtonItem.title = NSLocalizedString(@"Master", @"Master");
  319.     [self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
  320.     self.masterPopoverController = popoverController;
  321. }
  322.  
  323. - (void)splitViewController:(UISplitViewController *)splitController willShowViewController:(UIViewController *)viewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
  324. {
  325.     // Called when the view is shown again in the split view, invalidating the button and popover controller.
  326.     [self.navigationItem setLeftBarButtonItem:nil animated:YES];
  327.     self.masterPopoverController = nil;
  328. }
  329.  
  330. @end
  331.        
  332. #import <UIKit/UIKit.h>
  333.  
  334. @interface messages : UITableViewController{
  335.  
  336.     NSMutableArray *mesid,*contents;
  337.  
  338. }
  339.  
  340. @property(retain,nonatomic)NSMutableArray *mesid,*contents;
  341.  
  342.  
  343. @end
  344.        
  345. #import "messages.h"
  346. #import "messageDetail.h"
  347. @implementation messages
  348. @synthesize mesid;
  349. - (id)initWithStyle:(UITableViewStyle)style
  350. {
  351.     self = [super initWithStyle:style];
  352.     if (self) {
  353.         // Custom initialization
  354.     }
  355.     return self;
  356. }
  357.  
  358. - (void)didReceiveMemoryWarning
  359. {
  360.     // Releases the view if it doesn't have a superview.
  361.     [super didReceiveMemoryWarning];
  362.  
  363.     // Release any cached data, images, etc that aren't in use.
  364. }
  365.  
  366. #pragma mark - View lifecycle
  367.  
  368. - (void)viewDidLoad
  369. {
  370.     [super viewDidLoad];
  371.  
  372.     // Uncomment the following line to preserve selection between presentations.
  373.      self.clearsSelectionOnViewWillAppear = NO;
  374.  
  375.     // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
  376.      self.navigationItem.rightBarButtonItem = self.editButtonItem;
  377. }
  378.  
  379. - (void)viewDidUnload
  380. {
  381.     [super viewDidUnload];
  382.     // Release any retained subviews of the main view.
  383.     // e.g. self.myOutlet = nil;
  384. }
  385.  
  386. - (void)viewWillAppear:(BOOL)animated
  387. {
  388.     }
  389.  
  390. - (void)viewDidAppear:(BOOL)animated
  391. {
  392.     [super viewDidAppear:animated];
  393. }
  394.  
  395. - (void)viewWillDisappear:(BOOL)animated
  396. {
  397.     [super viewWillDisappear:animated];
  398. }
  399.  
  400. - (void)viewDidDisappear:(BOOL)animated
  401. {
  402.     [super viewDidDisappear:animated];
  403. }
  404.  
  405. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  406. {
  407.     // Return YES for supported orientations
  408.     return YES;
  409. }
  410.  
  411. #pragma mark - Table view data source
  412.  
  413. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  414. {
  415. #warning Potentially incomplete method implementation.
  416.     // Return the number of sections.
  417.     return 1;
  418. }
  419.  
  420. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  421. {
  422. #warning Incomplete method implementation.
  423.     // Return the number of rows in the section.
  424.  
  425.    return [mesid count];
  426. }
  427.  
  428. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  429. {
  430.     static NSString *CellIdentifier = @"Cell";
  431.  
  432.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  433.     if (cell == nil) {
  434.         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
  435.     }
  436.     cell.textLabel.text=[mesid objectAtIndex:indexPath.row];
  437.     [tableView reloadData];
  438.     // Configure the cell...
  439.  
  440.     return cell;
  441. }
  442.  
  443.  
  444. // Override to support conditional editing of the table view.
  445. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  446. {
  447.     // Return NO if you do not want the specified item to be editable.
  448.     return YES;
  449. }
  450.  
  451.  
  452.  
  453. // Override to support editing the table view.
  454. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  455. {
  456.     if (editingStyle == UITableViewCellEditingStyleDelete) {
  457.         // Delete the row from the data source
  458.         [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
  459.     }  
  460.     else if (editingStyle == UITableViewCellEditingStyleInsert) {
  461.         // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
  462.     }  
  463. }
  464.  
  465.  
  466. /*
  467. // Override to support rearranging the table view.
  468. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
  469. {
  470. }
  471. */
  472.  
  473. /*
  474. // Override to support conditional rearranging of the table view.
  475. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
  476. {
  477.     // Return NO if you do not want the item to be re-orderable.
  478.     return YES;
  479. }
  480. */
  481.  
  482. #pragma mark - Table view delegate
  483.  
  484. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  485. {
  486.     // Navigation logic may go here. Create and push another view controller.
  487.  
  488.      messageDetail *detailViewController = [[messageDetail alloc] initWithNibName:@"messageDetail" bundle:nil];
  489.      // ...
  490.      // Pass the selected object to the new view controller.
  491.      [self.navigationController pushViewController:detailViewController animated:YES];
  492.      detailViewController.title=@"Message Detail";
  493.      [detailViewController release];
  494.  
  495. }
  496.  
  497. @end
  498.        
  499. #import <UIKit/UIKit.h>
  500.  
  501. @interface messageDetail : UIViewController
  502.  
  503. @end
  504.        
  505. #import "messageDetail.h"
  506.  
  507. @implementation messageDetail
  508.  
  509. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  510. {
  511.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  512.     if (self) {
  513.         // Custom initialization
  514.     }
  515.     return self;
  516. }
  517.  
  518. - (void)didReceiveMemoryWarning
  519. {
  520.     // Releases the view if it doesn't have a superview.
  521.     [super didReceiveMemoryWarning];
  522.  
  523.     // Release any cached data, images, etc that aren't in use.
  524. }
  525.  
  526. #pragma mark - View lifecycle
  527.  
  528. - (void)viewDidLoad
  529. {
  530.     [super viewDidLoad];
  531.     // Do any additional setup after loading the view from its nib.
  532. }
  533.  
  534. - (void)viewDidUnload
  535. {
  536.     [super viewDidUnload];
  537.     // Release any retained subviews of the main view.
  538.     // e.g. self.myOutlet = nil;
  539. }
  540.  
  541. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  542. {
  543.     // Return YES for supported orientations
  544.     return YES;
  545. }
  546.  
  547. @end