PageRenderTime 29ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 1ms

/UICatalog/ButtonsViewController.m

https://gitlab.com/praveenvelanati/ios-demo
Objective C | 449 lines | 296 code | 77 blank | 76 comment | 23 complexity | 0e5868370c15a26ceb3f01c1f2d9bee2 MD5 | raw file
  1. /*
  2. File: ButtonsViewController.m
  3. Abstract: The table view controller for hosting the UIButton 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 "ButtonsViewController.h"
  42. #import "Constants.h"
  43. #define kStdButtonWidth 106.0
  44. #define kStdButtonHeight 40.0
  45. #define kViewTag 1 // for tagging our embedded controls for removal at cell recycle time
  46. static NSString *kSectionTitleKey = @"sectionTitleKey";
  47. static NSString *kLabelKey = @"labelKey";
  48. static NSString *kSourceKey = @"sourceKey";
  49. static NSString *kViewKey = @"viewKey";
  50. #pragma mark -
  51. @implementation ButtonsViewController
  52. @synthesize dataSourceArray;
  53. @synthesize grayButton, imageButton, roundedButtonType, detailDisclosureButtonType, infoLightButtonType, infoDarkButtonType, contactAddButtonType;
  54. - (void)dealloc
  55. {
  56. [grayButton release];
  57. [imageButton release];
  58. [roundedButtonType release];
  59. [detailDisclosureButtonType release];
  60. [infoLightButtonType release];
  61. [infoDarkButtonType release];
  62. [contactAddButtonType release];
  63. [dataSourceArray release];
  64. [super dealloc];
  65. }
  66. - (void)viewDidLoad
  67. {
  68. [super viewDidLoad];
  69. self.title = NSLocalizedString(@"ButtonsTitle", @"");
  70. self.dataSourceArray = [NSArray arrayWithObjects:
  71. [NSDictionary dictionaryWithObjectsAndKeys:
  72. @"UIButton", kSectionTitleKey,
  73. @"Background Image", kLabelKey,
  74. @"ButtonsViewController.m:\r(UIButton *)grayButton", kSourceKey,
  75. self.grayButton, kViewKey,
  76. nil],
  77. [NSDictionary dictionaryWithObjectsAndKeys:
  78. @"UIButton", kSectionTitleKey,
  79. @"Button with Image", kLabelKey,
  80. @"ButtonsViewController.m:\r(UIButton *)imageButton", kSourceKey,
  81. self.imageButton, kViewKey,
  82. nil],
  83. [NSDictionary dictionaryWithObjectsAndKeys:
  84. @"UIButtonTypeRoundedRect", kSectionTitleKey,
  85. @"Rounded Button", kLabelKey,
  86. @"ButtonsViewController.m:\r(UIButton *)roundedButtonType", kSourceKey,
  87. self.roundedButtonType, kViewKey,
  88. nil],
  89. [NSDictionary dictionaryWithObjectsAndKeys:
  90. @"UIButtonTypeDetailDisclosure", kSectionTitleKey,
  91. @"Detail Disclosure", kLabelKey,
  92. @"ButtonsViewController.m:\r(UIButton *)detailDisclosureButton", kSourceKey,
  93. self.detailDisclosureButtonType, kViewKey,
  94. nil],
  95. [NSDictionary dictionaryWithObjectsAndKeys:
  96. @"UIButtonTypeInfoLight", kSectionTitleKey,
  97. @"Info Light", kLabelKey,
  98. @"ButtonsViewController.m:\r(UIButton *)infoLightButtonType", kSourceKey,
  99. self.infoLightButtonType, kViewKey,
  100. nil],
  101. [NSDictionary dictionaryWithObjectsAndKeys:
  102. @"UIButtonTypeInfoDark", kSectionTitleKey,
  103. @"Info Dark", kLabelKey,
  104. @"ButtonsViewController.m:\r(UIButton *)infoDarkButtonType", kSourceKey,
  105. self.infoDarkButtonType, kViewKey,
  106. nil],
  107. [NSDictionary dictionaryWithObjectsAndKeys:
  108. @"UIButtonTypeContactAdd", kSectionTitleKey,
  109. @"Contact Add", kLabelKey,
  110. @"ButtonsViewController.m:\r(UIButton *)contactAddButtonType", kSourceKey,
  111. self.contactAddButtonType, kViewKey,
  112. nil],
  113. nil];
  114. }
  115. // called after the view controller's view is released and set to nil.
  116. // For example, a memory warning which causes the view to be purged. Not invoked as a result of -dealloc.
  117. // So release any properties that are loaded in viewDidLoad or can be recreated lazily.
  118. //
  119. - (void)viewDidUnload
  120. {
  121. [super viewDidUnload];
  122. // release the controls and set them nil in case they were ever created
  123. // note: we can't use "self.xxx = nil" since they are read only properties
  124. //
  125. [grayButton release];
  126. grayButton = nil;
  127. [imageButton release];
  128. imageButton = nil;
  129. [roundedButtonType release];
  130. roundedButtonType = nil;
  131. [detailDisclosureButtonType release];
  132. detailDisclosureButtonType = nil;
  133. [infoLightButtonType release];
  134. infoLightButtonType = nil;
  135. [infoDarkButtonType release];
  136. infoDarkButtonType = nil;
  137. [contactAddButtonType release];
  138. contactAddButtonType = nil;
  139. self.dataSourceArray = nil;
  140. }
  141. #pragma mark -
  142. #pragma mark UITableViewDataSource
  143. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  144. {
  145. return [self.dataSourceArray count];
  146. }
  147. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  148. {
  149. return [[self.dataSourceArray objectAtIndex: section] valueForKey:kSectionTitleKey];
  150. }
  151. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  152. {
  153. return 2;
  154. }
  155. // to determine specific row height for each cell, override this.
  156. // In this example, each row is determined by its subviews that are embedded.
  157. //
  158. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  159. {
  160. return ([indexPath row] == 0) ? 50.0 : 38.0;
  161. }
  162. // to determine which UITableViewCell to be used on a given row.
  163. //
  164. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  165. {
  166. UITableViewCell *cell = nil;
  167. if ([indexPath row] == 0)
  168. {
  169. static NSString *kDisplayCell_ID = @"DisplayCellID";
  170. cell = [self.tableView dequeueReusableCellWithIdentifier:kDisplayCell_ID];
  171. if (cell == nil)
  172. {
  173. cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kDisplayCell_ID] autorelease];
  174. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  175. }
  176. else
  177. {
  178. // the cell is being recycled, remove old embedded controls
  179. UIView *viewToRemove = nil;
  180. viewToRemove = [cell.contentView viewWithTag:kViewTag];
  181. if (viewToRemove)
  182. [viewToRemove removeFromSuperview];
  183. }
  184. cell.textLabel.text = [[self.dataSourceArray objectAtIndex: indexPath.section] valueForKey:kLabelKey];
  185. UIButton *button = [[self.dataSourceArray objectAtIndex: indexPath.section] valueForKey:kViewKey];
  186. [cell.contentView addSubview:button];
  187. }
  188. else
  189. {
  190. static NSString *kSourceCellID = @"SourceCellID";
  191. cell = [self.tableView dequeueReusableCellWithIdentifier:kSourceCellID];
  192. if (cell == nil)
  193. {
  194. cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kSourceCellID] autorelease];
  195. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  196. cell.textLabel.opaque = NO;
  197. cell.textLabel.textAlignment = UITextAlignmentCenter;
  198. cell.textLabel.textColor = [UIColor grayColor];
  199. cell.textLabel.numberOfLines = 2;
  200. cell.textLabel.highlightedTextColor = [UIColor blackColor];
  201. cell.textLabel.font = [UIFont systemFontOfSize:12.0];
  202. }
  203. cell.textLabel.text = [[self.dataSourceArray objectAtIndex: indexPath.section] valueForKey:kSourceKey];
  204. }
  205. return cell;
  206. }
  207. #pragma mark -
  208. + (UIButton *)newButtonWithTitle:(NSString *)title
  209. target:(id)target
  210. selector:(SEL)selector
  211. frame:(CGRect)frame
  212. image:(UIImage *)image
  213. imagePressed:(UIImage *)imagePressed
  214. darkTextColor:(BOOL)darkTextColor
  215. {
  216. UIButton *button = [[UIButton alloc] initWithFrame:frame];
  217. // or you can do this:
  218. // UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
  219. button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
  220. button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
  221. [button setTitle:title forState:UIControlStateNormal];
  222. if (darkTextColor)
  223. {
  224. [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  225. }
  226. else
  227. {
  228. [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  229. }
  230. UIImage *newImage = [image stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
  231. [button setBackgroundImage:newImage forState:UIControlStateNormal];
  232. UIImage *newPressedImage = [imagePressed stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
  233. [button setBackgroundImage:newPressedImage forState:UIControlStateHighlighted];
  234. [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
  235. // in case the parent view draws with a custom color or gradient, use a transparent color
  236. button.backgroundColor = [UIColor clearColor];
  237. return button;
  238. }
  239. - (void)action:(id)sender
  240. {
  241. //NSLog(@"UIButton was clicked");
  242. }
  243. #pragma mark -
  244. #pragma mark Lazy creation of buttons
  245. - (UIButton *)grayButton
  246. {
  247. if (grayButton == nil)
  248. {
  249. // create the UIButtons with various background images
  250. // white button:
  251. UIImage *buttonBackground = [UIImage imageNamed:@"whiteButton.png"];
  252. UIImage *buttonBackgroundPressed = [UIImage imageNamed:@"blueButton.png"];
  253. CGRect frame = CGRectMake(182.0, 5.0, kStdButtonWidth, kStdButtonHeight);
  254. grayButton = [ButtonsViewController newButtonWithTitle:@"Gray"
  255. target:self
  256. selector:@selector(action:)
  257. frame:frame
  258. image:buttonBackground
  259. imagePressed:buttonBackgroundPressed
  260. darkTextColor:YES];
  261. grayButton.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
  262. }
  263. return grayButton;
  264. }
  265. - (UIButton *)imageButton
  266. {
  267. if (imageButton == nil)
  268. {
  269. // create a UIButton with just an image instead of a title
  270. UIImage *buttonBackground = [UIImage imageNamed:@"whiteButton.png"];
  271. UIImage *buttonBackgroundPressed = [UIImage imageNamed:@"blueButton.png"];
  272. CGRect frame = CGRectMake(182.0, 5.0, kStdButtonWidth, kStdButtonHeight);
  273. imageButton = [ButtonsViewController newButtonWithTitle:@""
  274. target:self
  275. selector:@selector(action:)
  276. frame:frame
  277. image:buttonBackground
  278. imagePressed:buttonBackgroundPressed
  279. darkTextColor:YES];
  280. [imageButton setImage:[UIImage imageNamed:@"UIButton_custom.png"] forState:UIControlStateNormal];
  281. // Add an accessibility label to the image.
  282. [imageButton setAccessibilityLabel:NSLocalizedString(@"ArrowButton", @"")];
  283. imageButton.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
  284. }
  285. return imageButton;
  286. }
  287. - (UIButton *)roundedButtonType
  288. {
  289. if (roundedButtonType == nil)
  290. {
  291. // create a UIButton (UIButtonTypeRoundedRect)
  292. roundedButtonType = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
  293. roundedButtonType.frame = CGRectMake(182.0, 5.0, kStdButtonWidth, kStdButtonHeight);
  294. [roundedButtonType setTitle:@"Rounded" forState:UIControlStateNormal];
  295. roundedButtonType.backgroundColor = [UIColor clearColor];
  296. [roundedButtonType addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
  297. roundedButtonType.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
  298. }
  299. return roundedButtonType;
  300. }
  301. - (UIButton *)detailDisclosureButtonType
  302. {
  303. if (detailDisclosureButtonType == nil)
  304. {
  305. // create a UIButton (UIButtonTypeDetailDisclosure)
  306. detailDisclosureButtonType = [[UIButton buttonWithType:UIButtonTypeDetailDisclosure] retain];
  307. detailDisclosureButtonType.frame = CGRectMake(250.0, 8.0, 25.0, 25.0);
  308. [detailDisclosureButtonType setTitle:@"Detail Disclosure" forState:UIControlStateNormal];
  309. detailDisclosureButtonType.backgroundColor = [UIColor clearColor];
  310. [detailDisclosureButtonType addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
  311. // Add a custom accessibility label to the button because it has no associated text.
  312. [detailDisclosureButtonType setAccessibilityLabel:NSLocalizedString(@"MoreInfoButton", @"")];
  313. detailDisclosureButtonType.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
  314. }
  315. return detailDisclosureButtonType;
  316. }
  317. - (UIButton *)infoDarkButtonType
  318. {
  319. if (infoDarkButtonType == nil)
  320. {
  321. // create a UIButton (UIButtonTypeInfoLight)
  322. infoDarkButtonType = [[UIButton buttonWithType:UIButtonTypeInfoDark] retain];
  323. infoDarkButtonType.frame = CGRectMake(250.0, 8.0, 25.0, 25.0);
  324. [infoDarkButtonType setTitle:@"Detail Disclosure" forState:UIControlStateNormal];
  325. infoDarkButtonType.backgroundColor = [UIColor clearColor];
  326. [infoDarkButtonType addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
  327. // Add a custom accessibility label to the button because it has no associated text.
  328. [infoDarkButtonType setAccessibilityLabel:NSLocalizedString(@"MoreInfoButton", @"")];
  329. infoDarkButtonType.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
  330. }
  331. return infoDarkButtonType;
  332. }
  333. - (UIButton *)infoLightButtonType
  334. {
  335. if (infoLightButtonType == nil)
  336. {
  337. // create a UIButton (UIButtonTypeInfoLight)
  338. infoLightButtonType = [[UIButton buttonWithType:UIButtonTypeInfoLight] retain];
  339. infoLightButtonType.frame = CGRectMake(250.0, 8.0, 25.0, 25.0);
  340. [infoLightButtonType setTitle:@"Detail Disclosure" forState:UIControlStateNormal];
  341. infoLightButtonType.backgroundColor = [UIColor clearColor];
  342. [infoLightButtonType addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
  343. infoLightButtonType.backgroundColor = [UIColor grayColor];
  344. // Add a custom accessibility label to the button because it has no associated text.
  345. [infoLightButtonType setAccessibilityLabel:NSLocalizedString(@"MoreInfoButton", @"")];
  346. infoLightButtonType.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
  347. }
  348. return infoLightButtonType;
  349. }
  350. - (UIButton *)contactAddButtonType
  351. {
  352. if (contactAddButtonType == nil)
  353. {
  354. // create a UIButton (UIButtonTypeContactAdd)
  355. contactAddButtonType = [[UIButton buttonWithType:UIButtonTypeContactAdd] retain];
  356. contactAddButtonType.frame = CGRectMake(250.0, 8.0, 25.0, 25.0);
  357. [contactAddButtonType setTitle:@"Detail Disclosure" forState:UIControlStateNormal];
  358. contactAddButtonType.backgroundColor = [UIColor clearColor];
  359. [contactAddButtonType addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
  360. // Add a custom accessibility label to the button because it has no associated text.
  361. [contactAddButtonType setAccessibilityLabel:NSLocalizedString(@"AddContactButton", @"")];
  362. contactAddButtonType.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
  363. }
  364. return contactAddButtonType;
  365. }
  366. @end