Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.91 KB | None | 0 0
  1. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  2. info = [array objectAtIndex:indexPath.row];
  3. static NSString *cellIdentifier = @"empCell";
  4. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
  5. titlebutton = (UIButton *)[cell viewWithTag:100];
  6. [titlebutton addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
  7. [titlebutton setTitle:info.name forState:UIControlStateNormal];
  8. return cell;
  9. }
  10.  
  11. This question seems to be very popular here on stackoverflow so I thought I would try and give a better answer to help out people starting in the world of iOS like me.
  12.  
  13. I hope this answer is clear enough for people to understand and that I have not missed anything.
  14.  
  15. Passing Data Forward
  16.  
  17. Passing data forward to a view controller from another view controller. You would use this method if you wanted to pass an object/value from one view controller to another view controller that you may be pushing on to a navigation stack.
  18.  
  19. For this example we will have ViewControllerA and ViewControllerB
  20.  
  21. To pass a BOOL value from ViewControllerA to ViewControllerB we would do the following.
  22.  
  23. in ViewControllerB.h create a property for the BOOL
  24.  
  25. @property(nonatomic) BOOL *isSomethingEnabled;
  26. in ViewControllerA you need to tell it about ViewControllerB so use an
  27.  
  28. #import "ViewControllerB.h"
  29. Then where you want to load the view eg. didSelectRowAtIndex or some IBAction you need to set the property in ViewControllerB before you push it onto nav stack.
  30.  
  31. ViewControllerB *viewControllerB = [[ViewControllerB alloc] initWithNib:@"ViewControllerB" bundle:nil];
  32. viewControllerB.isSomethingEnabled = YES;
  33. [self pushViewController:viewControllerB animated:YES];
  34. This will set isSomethingEnabled in ViewControllerB to BOOL value YES.
  35.  
  36. Passing Data Forward using Segue's
  37.  
  38. If you are using Storyboards you are most likely using segues and will need this procedure to pass data forward. This is similar to the above but instead of passing the data before you push the view controller, you use a method called
  39.  
  40. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  41. So to pass a BOOL from ViewControllerA to ViewControllerB we would do the following:
  42.  
  43. in ViewControllerB.h create a property for the BOOL
  44.  
  45. @property(nonatomic) BOOL *isSomethingEnabled;
  46. in ViewControllerA you need to tell it about ViewControllerB so use an
  47.  
  48. #import "ViewControllerB.h"
  49. Create a the segue from ViewControllerA to ViewControllerB on the storyboard and give it an identifier, in this example we'll call it "showDetailSegue"
  50.  
  51. Next we need to add the method to ViewControllerA that is called when any segue is performed, because of this we need to detect which segue was called and then do something. In our example we will check for "showDetailSegue" and if thats performed we will pass our BOOL value to ViewControllerB
  52.  
  53. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
  54. if([segue.identifier isEqualToString:@"showDetailSegue"]){
  55. ViewControllerB *controller = (ViewControllerB *)segue.destinationViewController;
  56. controller.isSomethingEnabled = YES;
  57. }
  58. }
  59. If you have your views embedded in a navigation controller you need to change the method above slightly to the following
  60.  
  61. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
  62. if([segue.identifier isEqualToString:@"showDetailSegue"]){
  63. UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
  64. ViewControllerB *controller = (ViewControllerB *)navController.topViewController;
  65. controller.isSomethingEnabled = YES;
  66. }
  67. }
  68. This will set isSomethingEnabled in ViewControllerB to BOOL value YES.
  69.  
  70. Passing Data Back
  71.  
  72. To pass data back from ViewControllerB to ViewControllerA you need to use Protocols and Delegates or Blocks, the latter can be used as a loosely coupled mechanism for callbacks.
  73.  
  74. To do this we will make ViewControllerA a delegate of ViewControllerB. This allows ViewControllerB to send a message back to ViewControllerA enabling us to send data back.
  75.  
  76. For ViewControllerA to be delegate of ViewControllerB it must conform to ViewControllerB's protocol which we have to specify. This tells ViewControllerA which methods it must implement.
  77.  
  78. In ViewControllerB.h, below the #import, but above @interface you specify the protocol.
  79.  
  80. @class ViewControllerB;
  81.  
  82. @protocol ViewControllerBDelegate <NSObject>
  83. - (void)addItemViewController:(ViewControllerB *)controller didFinishEnteringItem:(NSString *)item;
  84. @end
  85. next still in the ViewControllerB.h you need to setup a delegate property and synthesize in ViewControllerB.m
  86.  
  87. @property (nonatomic, weak) id <ViewControllerBDelegate> delegate;
  88. In ViewControllerB we call a message on the delegate when we pop the view controller.
  89.  
  90. NSString *itemToPassBack = @"Pass this value back to ViewControllerA";
  91. [self.delegate addItemViewController:self didFinishEnteringItem:itemToPassBack];
  92. That's it for ViewControllerB. Now in ViewControllerA.h, tell ViewControllerA to import ViewControllerB and conform to its protocol.
  93.  
  94. #import "ViewControllerB.h"
  95.  
  96. @interface ViewControllerA : UIViewController <ViewControllerBDelegate>
  97. In ViewControllerA.m implement the following method from our protocol
  98.  
  99. - (void)addItemViewController:(ViewControllerB *)controller didFinishEnteringItem:(NSString *)item
  100. {
  101. NSLog(@"This was returned from ViewControllerB %@",item);
  102. }
  103. The last thing we need to do is tell ViewControllerB that ViewControllerA is its delegate before we push ViewControllerB on to nav stack.
  104.  
  105. ViewControllerB *viewControllerB = [[ViewControllerB alloc] initWithNib:@"ViewControllerB" bundle:nil];
  106. viewControllerB.delegate = self
  107. [[self navigationController] pushViewController:viewControllerB animated:YES];
  108. References
  109.  
  110. Using Delegation to Communicate With Other View Controllers in the View Controller Programming Guide
  111. Delegate Pattern
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement