Guest User

Untitled

a guest
Jan 26th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.48 KB | None | 0 0
  1. //
  2. // PickerViewViewController.m
  3. // PickerView
  4. //
  5. // Created by Ciro Xavier on 3/16/11.
  6. // Copyright 2011 __MyCompanyName__. All rights reserved.
  7. //
  8.  
  9. #import "PickerViewViewController.h"
  10.  
  11. @implementation PickerViewViewController
  12.  
  13. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
  14. {
  15. return 2;
  16. }
  17.  
  18.  
  19. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
  20. {
  21. if(component == 0)
  22. {
  23. return [continente count];
  24. }
  25. else if (component == 1)
  26. {
  27. // pega o array de paises do continente selecionado
  28. NSMutableDictionary *d = [continente objectAtIndex:selectedContinent];
  29. NSMutableArray *paises = [d objectForKey:@"paises"];
  30.  
  31. // retorna o numero de paises do vetor
  32. return [paises count];
  33. }
  34. else
  35. {
  36. NSLog(@"Componente invalido, component=%d",component);
  37. }
  38.  
  39.  
  40. return 0;
  41. }
  42.  
  43. - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
  44. {
  45. if (component == 0)
  46. {
  47. // nome do continente
  48. return [[continente objectAtIndex:row] objectForKey:@"nome"];
  49. }
  50. else if (component == 1)
  51. {
  52. // pegar vetor de paises
  53. NSMutableDictionary *d = [continente objectAtIndex:selectedContinent];
  54. NSMutableArray *paises = [d objectForKey:@"paises"];
  55.  
  56. // retorna o nome do pais
  57. return [[paises objectAtIndex:row] objectForKey:@"nome"];
  58. }
  59.  
  60. return nil;
  61. }
  62.  
  63. - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
  64. {
  65. if (component == 0)
  66. {
  67. selectedContinent = row;
  68.  
  69. // recarrega os dados da coluna da direita
  70. [pv reloadComponent:1];
  71. [pv selectRow:0 inComponent:1 animated:YES];
  72.  
  73. // pega o nome da imagem
  74. NSMutableDictionary *dadosContinente = [continente objectAtIndex:selectedContinent];
  75. NSMutableArray *paisesDoContinente = [dadosContinente objectForKey:@"paises"];
  76. NSMutableDictionary *dadosPais = [paisesDoContinente objectAtIndex:0];
  77. NSString *nomeDaImagem = [dadosPais objectForKey:@"imagem"];
  78.  
  79. // carrega imagem
  80. UIImage *img = [UIImage imageNamed:nomeDaImagem];
  81. // colocar a imagem no imageView
  82. imgv.image = img;
  83. }
  84. else if (component == 1)
  85. {
  86. // pega o nome da imagem
  87. NSMutableDictionary *dadosContinente = [continente objectAtIndex:selectedContinent];
  88.  
  89. NSMutableArray *paisesDoContinente = [dadosContinente objectForKey:@"paises"];
  90.  
  91. NSMutableDictionary *dadosPais = [paisesDoContinente objectAtIndex:row];
  92.  
  93. NSString *nomeDaImagem = [dadosPais objectForKey:@"imagem"];
  94.  
  95. // carrega a imagem
  96. UIImage *img = [UIImage imageNamed:nomeDaImagem];
  97. // colocar a imagem no imageView
  98. imgv.image = img;
  99.  
  100. }
  101.  
  102.  
  103. }
  104.  
  105. /*
  106. // The designated initializer. Override to perform setup that is required before the view is loaded.
  107. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  108. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  109. if (self) {
  110. // Custom initialization
  111. }
  112. return self;
  113. }
  114. */
  115.  
  116. /*
  117. // Implement loadView to create a view hierarchy programmatically, without using a nib.
  118. - (void)loadView {
  119. }
  120. */
  121.  
  122.  
  123.  
  124. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
  125. - (void)viewDidLoad {
  126. [super viewDidLoad];
  127.  
  128. // alocar o array
  129. continente = [[NSMutableArray alloc] init];
  130.  
  131. // ********** africa
  132. NSMutableDictionary *cont = [[[NSMutableDictionary alloc] init] autorelease];
  133.  
  134. NSMutableArray *paises = [[[NSMutableArray alloc] init] autorelease];
  135.  
  136. // cria o camaroes
  137. NSMutableDictionary *pais = [[[NSMutableDictionary alloc] init] autorelease];
  138.  
  139. [pais setObject:@"Cameroon" forKey:@"nome"];
  140. [pais setObject:@"Cameroon.gif" forKey:@"imagem"];
  141.  
  142. [paises addObject:pais];
  143.  
  144. // cria a africa do sul
  145. pais = [[[NSMutableDictionary alloc] init] autorelease];
  146.  
  147. [pais setObject:@"South Africa" forKey:@"nome"];
  148. [pais setObject:@"South Africa.gif" forKey:@"imagem"];
  149.  
  150. [paises addObject:pais];
  151.  
  152. // adiciona lista de paises ao continente
  153. [cont setObject:paises forKey:@"paises"];
  154. [cont setObject:@"Africa" forKey:@"nome"];
  155.  
  156. [continente addObject:cont];
  157.  
  158. // *********** america
  159. cont = [[[NSMutableDictionary alloc] init] autorelease];
  160.  
  161. paises = [[[NSMutableArray alloc] init] autorelease];
  162.  
  163. // cria o brasil
  164. pais = [[[NSMutableDictionary alloc] init] autorelease];
  165. [pais setObject:@"Brasil" forKey:@"nome"];
  166. [pais setObject:@"Brazil.gif" forKey:@"imagem"];
  167.  
  168. [paises addObject:pais];
  169.  
  170. // cria o EUA
  171. pais = [[[NSMutableDictionary alloc] init] autorelease];
  172. [pais setObject:@"Estados Unidos" forKey:@"nome"];
  173. [pais setObject:@"USA.gif" forKey:@"imagem"];
  174.  
  175. [paises addObject:pais];
  176.  
  177. // cria o Mexico
  178. pais = [[[NSMutableDictionary alloc] init] autorelease];
  179. [pais setObject:@"Mexico" forKey:@"nome"];
  180. [pais setObject:@"Mexico.gif" forKey:@"imagem"];
  181.  
  182. [paises addObject:pais];
  183.  
  184. // adiciona lista de paises ao continente
  185. [cont setObject:paises forKey:paises];
  186. [cont setObject:@"America" forKey:@"nome"];
  187.  
  188. [continente addObject:cont];
  189.  
  190. // *********** europa
  191. cont = [[[NSMutableDictionary alloc] init] autorelease];
  192.  
  193. paises = [[[NSMutableArray alloc] init] autorelease];
  194.  
  195. // cria o italia
  196. pais = [[[NSMutableDictionary alloc] init] autorelease];
  197. [pais setObject:@"Italia" forKey:@"nome"];
  198. [pais setObject:@"Italy.gif" forKey:@"imagem"];
  199.  
  200. [paises addObject:pais];
  201.  
  202. // cria o Portugal
  203. pais = [[[NSMutableDictionary alloc] init] autorelease];
  204. [pais setObject:@"Portugal" forKey:@"nome"];
  205. [pais setObject:@"Portugal.gif" forKey:@"imagem"];
  206.  
  207. [paises addObject:pais];
  208.  
  209. // adiciona lista de paises ao continente
  210. [cont setObject:paises forKey:paises];
  211. [cont setObject:@"Europa" forKey:@"nome"];
  212.  
  213. [continente addObject:cont];
  214.  
  215. selectedContinent = 0;
  216.  
  217. }
  218.  
  219.  
  220.  
  221. /*
  222. // Override to allow orientations other than the default portrait orientation.
  223. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  224. // Return YES for supported orientations
  225. return (interfaceOrientation == UIInterfaceOrientationPortrait);
  226. }
  227. */
  228.  
  229. - (void)didReceiveMemoryWarning {
  230. // Releases the view if it doesn't have a superview.
  231. [super didReceiveMemoryWarning];
  232.  
  233. // Release any cached data, images, etc that aren't in use.
  234. }
  235.  
  236. - (void)viewDidUnload {
  237. // Release any retained subviews of the main view.
  238. // e.g. self.myOutlet = nil;
  239. }
  240.  
  241.  
  242. - (void)dealloc {
  243. [super dealloc];
  244. }
  245.  
  246. @end
Add Comment
Please, Sign In to add comment