PageRenderTime 25ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/UICatalog/ControlsViewController.m

https://gitlab.com/praveenvelanati/ios-demo
Objective C | 453 lines | 306 code | 73 blank | 74 comment | 27 complexity | b790025bade83063aefa1dc15211de23 MD5 | raw file
  1. /*
  2. File: ControlsViewController.m
  3. Abstract: The view controller for hosting the UIControls features of this sample.
  4. Version: 2.10
  5. Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
  6. Inc. ("Apple") in consideration of your agreement to the following
  7. terms, and your use, installation, modification or redistribution of
  8. this Apple software constitutes acceptance of these terms. If you do
  9. not agree with these terms, please do not use, install, modify or
  10. redistribute this Apple software.
  11. In consideration of your agreement to abide by the following terms, and
  12. subject to these terms, Apple grants you a personal, non-exclusive
  13. license, under Apple's copyrights in this original Apple software (the
  14. "Apple Software"), to use, reproduce, modify and redistribute the Apple
  15. Software, with or without modifications, in source and/or binary forms;
  16. provided that if you redistribute the Apple Software in its entirety and
  17. without modifications, you must retain this notice and the following
  18. text and disclaimers in all such redistributions of the Apple Software.
  19. Neither the name, trademarks, service marks or logos of Apple Inc. may
  20. be used to endorse or promote products derived from the Apple Software
  21. without specific prior written permission from Apple. Except as
  22. expressly stated in this notice, no other rights or licenses, express or
  23. implied, are granted by Apple herein, including but not limited to any
  24. patent rights that may be infringed by your derivative works or by other
  25. works in which the Apple Software may be incorporated.
  26. The Apple Software is provided by Apple on an "AS IS" basis. APPLE
  27. MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
  28. THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
  29. FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
  30. OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
  31. IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
  32. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  33. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  34. INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
  35. MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
  36. AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
  37. STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
  38. POSSIBILITY OF SUCH DAMAGE.
  39. Copyright (C) 2011 Apple Inc. All Rights Reserved.
  40. */
  41. #import "ControlsViewController.h"
  42. #import "Constants.h"
  43. #define kSliderHeight 7.0
  44. #define kProgressIndicatorSize 40.0
  45. #define kUIProgressBarWidth 160.0
  46. #define kUIProgressBarHeight 24.0
  47. #define kViewTag 1 // for tagging our embedded controls for removal at cell recycle time
  48. static NSString *kSectionTitleKey = @"sectionTitleKey";
  49. static NSString *kLabelKey = @"labelKey";
  50. static NSString *kSourceKey = @"sourceKey";
  51. static NSString *kViewKey = @"viewKey";
  52. #pragma mark -
  53. @interface ControlsViewController (forwardDeclarations)
  54. - (void)tintAction:(id)sender;
  55. @end
  56. @implementation ControlsViewController
  57. @synthesize dataSourceArray, switchCtl, sliderCtl, customSlider, pageControl, progressInd, progressBar, stepper, progressIndSavedColor;
  58. - (void)dealloc
  59. {
  60. [switchCtl release];
  61. [sliderCtl release];
  62. [customSlider release];
  63. [pageControl release];
  64. [progressInd release];
  65. [progressIndSavedColor release];
  66. [progressBar release];
  67. [stepper release];
  68. [dataSourceArray release];
  69. [super dealloc];
  70. }
  71. - (void)viewDidLoad
  72. {
  73. [super viewDidLoad];
  74. self.title = NSLocalizedString(@"ControlsTitle", @"");
  75. self.dataSourceArray = [NSMutableArray arrayWithObjects:
  76. [NSDictionary dictionaryWithObjectsAndKeys:
  77. @"UISwitch", kSectionTitleKey,
  78. @"Standard Switch", kLabelKey,
  79. @"ControlsViewController.m:\r-(UISwitch *)switchCtl", kSourceKey,
  80. self.switchCtl, kViewKey,
  81. nil],
  82. [NSDictionary dictionaryWithObjectsAndKeys:
  83. @"UISlider", kSectionTitleKey,
  84. @"Standard Slider", kLabelKey,
  85. @"ControlsViewController.m:\r-(UISlider *)sliderCtl", kSourceKey,
  86. self.sliderCtl, kViewKey,
  87. nil],
  88. [NSDictionary dictionaryWithObjectsAndKeys:
  89. @"UISlider", kSectionTitleKey,
  90. @"Customized Slider", kLabelKey,
  91. @"ControlsViewController.m:\r-(UISlider *)customSlider", kSourceKey,
  92. self.customSlider, kViewKey,
  93. nil],
  94. [NSDictionary dictionaryWithObjectsAndKeys:
  95. @"UIPageControl", kSectionTitleKey,
  96. @"Ten Pages", kLabelKey,
  97. @"ControlsViewController.m:\r-(UIPageControl *)pageControl", kSourceKey,
  98. self.pageControl, kViewKey,
  99. nil],
  100. [NSDictionary dictionaryWithObjectsAndKeys:
  101. @"UIActivityIndicatorView", kSectionTitleKey,
  102. @"Style Gray", kLabelKey,
  103. @"ControlsViewController.m:\r-(UIActivityIndicatorView *)progressInd", kSourceKey,
  104. self.progressInd, kViewKey,
  105. nil],
  106. [NSDictionary dictionaryWithObjectsAndKeys:
  107. @"UIProgressView", kSectionTitleKey,
  108. @"Style Default", kLabelKey,
  109. @"ControlsViewController.m:\r-(UIProgressView *)progressBar", kSourceKey,
  110. self.progressBar, kViewKey,
  111. nil],
  112. nil];
  113. // add a UIStepper class section (if available)
  114. if ([UIStepper class])
  115. {
  116. [self.dataSourceArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:
  117. @"UIStepper", kSectionTitleKey,
  118. @"Stepper 1 to 10", kLabelKey,
  119. @"ControlsViewController.m:\r-(UIStepper *)stepper", kSourceKey,
  120. self.stepper, kViewKey,
  121. nil]];
  122. }
  123. // provide tint coloring only if its available
  124. if ([sliderCtl respondsToSelector:@selector(minimumTrackTintColor)])
  125. {
  126. // add tint bar button
  127. UIBarButtonItem *tintButton = [[UIBarButtonItem alloc] initWithTitle:@"Tinted"
  128. style:UIBarButtonItemStyleBordered
  129. target:self
  130. action:@selector(tintAction:)];
  131. self.navigationItem.rightBarButtonItem = tintButton;
  132. [tintButton release];
  133. }
  134. }
  135. // called after the view controller's view is released and set to nil.
  136. // For example, a memory warning which causes the view to be purged. Not invoked as a result of -dealloc.
  137. // So release any properties that are loaded in viewDidLoad or can be recreated lazily.
  138. //
  139. - (void)viewDidUnload
  140. {
  141. [super viewDidUnload];
  142. // release the controls and set them nil in case they were ever created
  143. // note: we can't use "self.xxx = nil" since they are read only properties
  144. //
  145. [switchCtl release];
  146. switchCtl = nil;
  147. [sliderCtl release];
  148. sliderCtl = nil;
  149. [customSlider release];
  150. customSlider = nil;
  151. [pageControl release];
  152. pageControl = nil;
  153. [progressInd release];
  154. progressInd = nil;
  155. [progressBar release];
  156. progressBar = nil;
  157. [stepper release];
  158. stepper = nil;
  159. self.dataSourceArray = nil; // this will release and set to nil
  160. }
  161. #pragma mark -
  162. #pragma mark UITableViewDataSource
  163. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  164. {
  165. return [self.dataSourceArray count];
  166. }
  167. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  168. {
  169. return [[self.dataSourceArray objectAtIndex: section] valueForKey:kSectionTitleKey];
  170. }
  171. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  172. {
  173. return 2;
  174. }
  175. // to determine specific row height for each cell, override this.
  176. // In this example, each row is determined by its subviews that are embedded.
  177. //
  178. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  179. {
  180. return ([indexPath row] == 0) ? 50.0 : 38.0;
  181. }
  182. // to determine which UITableViewCell to be used on a given row.
  183. //
  184. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  185. {
  186. UITableViewCell *cell = nil;
  187. if ([indexPath row] == 0)
  188. {
  189. static NSString *kDisplayCell_ID = @"DisplayCellID";
  190. cell = [self.tableView dequeueReusableCellWithIdentifier:kDisplayCell_ID];
  191. if (cell == nil)
  192. {
  193. cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kDisplayCell_ID] autorelease];
  194. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  195. }
  196. else
  197. {
  198. // the cell is being recycled, remove old embedded controls
  199. UIView *viewToRemove = nil;
  200. viewToRemove = [cell.contentView viewWithTag:kViewTag];
  201. if (viewToRemove)
  202. [viewToRemove removeFromSuperview];
  203. }
  204. cell.textLabel.text = [[self.dataSourceArray objectAtIndex: indexPath.section] valueForKey:kLabelKey];
  205. UIControl *control = [[self.dataSourceArray objectAtIndex: indexPath.section] valueForKey:kViewKey];
  206. [cell.contentView addSubview:control];
  207. }
  208. else
  209. {
  210. static NSString *kSourceCellID = @"SourceCellID";
  211. cell = [self.tableView dequeueReusableCellWithIdentifier:kSourceCellID];
  212. if (cell == nil)
  213. {
  214. cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kSourceCellID] autorelease];
  215. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  216. cell.textLabel.opaque = NO;
  217. cell.textLabel.textAlignment = UITextAlignmentCenter;
  218. cell.textLabel.textColor = [UIColor grayColor];
  219. cell.textLabel.numberOfLines = 2;
  220. cell.textLabel.highlightedTextColor = [UIColor blackColor];
  221. cell.textLabel.font = [UIFont systemFontOfSize:12.0];
  222. }
  223. cell.textLabel.text = [[self.dataSourceArray objectAtIndex: indexPath.section] valueForKey:kSourceKey];
  224. }
  225. return cell;
  226. }
  227. - (void)switchAction:(id)sender
  228. {
  229. // NSLog(@"switchAction: value = %d", [sender isOn]);
  230. }
  231. - (void)pageAction:(id)sender
  232. {
  233. // NSLog(@"pageAction: current page = %d", [sender currentPage]);
  234. }
  235. - (void)sliderAction:(id)sender
  236. {
  237. // UISlider *slider = (UISlider *)sender;
  238. // NSLog(@"sliderAction: value = %f", [slider value]);
  239. }
  240. - (void)stepperAction:(id)sender
  241. {
  242. // UIStepper *actualStepper = (UIStepper *)sender;
  243. // NSLog(@"stepperAction: value = %f", [actualStepper value]);
  244. }
  245. #pragma mark -
  246. #pragma mark Lazy creation of controls
  247. - (UISwitch *)switchCtl
  248. {
  249. if (switchCtl == nil)
  250. {
  251. CGRect frame = CGRectMake(198.0, 12.0, 94.0, 27.0);
  252. switchCtl = [[UISwitch alloc] initWithFrame:frame];
  253. [switchCtl addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
  254. // in case the parent view draws with a custom color or gradient, use a transparent color
  255. switchCtl.backgroundColor = [UIColor clearColor];
  256. [switchCtl setAccessibilityLabel:NSLocalizedString(@"StandardSwitch", @"")];
  257. switchCtl.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
  258. }
  259. return switchCtl;
  260. }
  261. - (UISlider *)sliderCtl
  262. {
  263. if (sliderCtl == nil)
  264. {
  265. CGRect frame = CGRectMake(174.0, 12.0, 120.0, kSliderHeight);
  266. sliderCtl = [[UISlider alloc] initWithFrame:frame];
  267. [sliderCtl addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
  268. // in case the parent view draws with a custom color or gradient, use a transparent color
  269. sliderCtl.backgroundColor = [UIColor clearColor];
  270. sliderCtl.minimumValue = 0.0;
  271. sliderCtl.maximumValue = 100.0;
  272. sliderCtl.continuous = YES;
  273. sliderCtl.value = 50.0;
  274. // Add an accessibility label that describes the slider.
  275. [sliderCtl setAccessibilityLabel:NSLocalizedString(@"StandardSlider", @"")];
  276. sliderCtl.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
  277. }
  278. return sliderCtl;
  279. }
  280. - (UISlider *)customSlider
  281. {
  282. if (customSlider == nil)
  283. {
  284. CGRect frame = CGRectMake(174, 12.0, 120.0, kSliderHeight);
  285. customSlider = [[UISlider alloc] initWithFrame:frame];
  286. [customSlider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
  287. // in case the parent view draws with a custom color or gradient, use a transparent color
  288. customSlider.backgroundColor = [UIColor clearColor];
  289. UIImage *stetchLeftTrack = [[UIImage imageNamed:@"orangeslide.png"]
  290. stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
  291. UIImage *stetchRightTrack = [[UIImage imageNamed:@"yellowslide.png"]
  292. stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
  293. [customSlider setThumbImage: [UIImage imageNamed:@"slider_ball.png"] forState:UIControlStateNormal];
  294. [customSlider setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];
  295. [customSlider setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];
  296. customSlider.minimumValue = 0.0;
  297. customSlider.maximumValue = 100.0;
  298. customSlider.continuous = YES;
  299. customSlider.value = 50.0;
  300. // Add an accessibility label that describes the slider.
  301. [customSlider setAccessibilityLabel:NSLocalizedString(@"CustomSlider", @"")];
  302. customSlider.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
  303. }
  304. return customSlider;
  305. }
  306. - (UIPageControl *)pageControl
  307. {
  308. if (pageControl == nil)
  309. {
  310. CGRect frame = CGRectMake(120.0, 14.0, 178.0, 20.0);
  311. pageControl = [[UIPageControl alloc] initWithFrame:frame];
  312. [pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventTouchUpInside];
  313. // in case the parent view draws with a custom color or gradient, use a transparent color
  314. pageControl.backgroundColor = [UIColor grayColor];
  315. pageControl.numberOfPages = 10; // must be set or control won't draw
  316. pageControl.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
  317. }
  318. return pageControl;
  319. }
  320. - (UIActivityIndicatorView *)progressInd
  321. {
  322. if (progressInd == nil)
  323. {
  324. CGRect frame = CGRectMake(265.0, 12.0, kProgressIndicatorSize, kProgressIndicatorSize);
  325. progressInd = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
  326. self.progressIndSavedColor = progressInd.color;
  327. progressInd.frame = frame;
  328. [progressInd startAnimating];
  329. progressInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
  330. [progressInd sizeToFit];
  331. progressInd.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
  332. }
  333. return progressInd;
  334. }
  335. - (UIProgressView *)progressBar
  336. {
  337. if (progressBar == nil)
  338. {
  339. CGRect frame = CGRectMake(126.0, 20.0, kUIProgressBarWidth, kUIProgressBarHeight);
  340. progressBar = [[UIProgressView alloc] initWithFrame:frame];
  341. progressBar.progressViewStyle = UIProgressViewStyleDefault;
  342. progressBar.progress = 0.5;
  343. progressBar.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
  344. }
  345. return progressBar;
  346. }
  347. - (UIStepper *)stepper
  348. {
  349. if (stepper == nil && [UIStepper class])
  350. {
  351. CGRect frame = CGRectMake(200.0, 10.0, 0.0, 0.0);
  352. stepper = [[UIStepper alloc] initWithFrame:frame];
  353. [stepper sizeToFit]; // size the control to it's normal size
  354. stepper.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
  355. stepper.value = 0;
  356. stepper.minimumValue = 0;
  357. stepper.maximumValue = 10;
  358. stepper.stepValue = 1;
  359. [stepper addTarget:self action:@selector(stepperAction:) forControlEvents:UIControlEventValueChanged];
  360. }
  361. return stepper;
  362. }
  363. - (void)tintAction:(id)sender
  364. {
  365. UIColor *tintColor = (progressBar.progressTintColor != nil) ? nil : [UIColor blueColor];
  366. progressBar.progressTintColor = tintColor;
  367. progressBar.trackTintColor = tintColor;
  368. sliderCtl.minimumTrackTintColor = tintColor;
  369. sliderCtl.thumbTintColor = tintColor;
  370. switchCtl.onTintColor = tintColor;
  371. UIColor *progressIndColor = (progressInd.color != progressIndSavedColor) ? self.progressIndSavedColor : [UIColor blueColor];
  372. progressInd.color = progressIndColor;
  373. }
  374. @end