Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Let's say you have a <code>CatalogViewController</code> (which is a subclass of <code>UITableViewController</code>) and a <code>ProductViewController</code> (which is a subclass of <code>UIViewController</code>).</p> <p>The simplest way to implement “swipe to next product” is to give the <code>ProductViewController</code> a reference to the <code>CatalogViewController</code>. It should be <code>weak</code> (if using ARC) or <code>assign</code> (if not using ARC). You will also want a property that holds the index of the product in the catalog:</p> <pre><code>@interface ProductViewController @property (nonatomic, weak) CatalogViewController *catalogViewController; @property (nonatomic) NSInteger productIndex; @end </code></pre> <p>Then in the action method for a swipe, you send a message to the <code>CatalogViewController</code> asking it to go to the next (or prior) product in the catalog:</p> <pre><code>@implementation ProductViewController - (IBAction)showNextProduct:(id)sender { [self.catalogViewController goToProductAtIndex:self.productIndex + 1]; } - (IBAction)showPriorProduct:(id)sender { [self.catalogViewController goToProductAtIndex:self.productIndex - 1]; } </code></pre> <p>In <code>CatalogViewController</code>, whenever you create a <code>ProductViewController</code>, you need to set those properties:</p> <pre><code>@implementation CatalogViewController - (ProductViewController *)productViewControllerForProductAtIndex:(NSInteger)index { if (index &lt; 0 || index &gt;= self.products.count) return nil; ProductViewController *vc = [[ProductViewController alloc] initWithProduct:[self.products objectAtIndex:index]]; vc.catalogViewController = self; vc.productIndex = index; return vc; } </code></pre> <p>and you implement the <code>goToProductAtIndex:</code> method like this:</p> <pre><code>- (void)goToProductAtIndex:(NSInteger)index { ProductViewController *vc = [self productViewControllerForProductAtIndex:index]; if (!vc) return; NSMutableArray *vcs = [[self.navigationController viewControllers] mutableCopy]; while (vcs.lastObject != self) [vcs removeLastObject]; [vcs addObject:vc]; [self.navigationController setViewControllers:vcs animated:YES]; } </code></pre> <p>You can use the same method to handle a table row selection:</p> <pre><code>- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self goToProductAtIndex:indexPath.row]; } </code></pre> <p>If you want to get more software-engineery, you can create a protocol around the <code>goToProductAtIndex:</code> method and use that to avoid making <code>ProductViewController</code> know about the <code>CatalogViewController</code> class.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload