Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdating detailDescriptionLabel data from UITableView selected index
    primarykey
    data
    text
    <p>I have a split view controller for iPad with a drill-down table on the left. I am able to populate the tables with a drill-down without issue but I cannot seem to get the item I clicked within the UITableView on the left side to show up in detailDescriptionLabel on the right side.</p> <p>I have the following code in my ProductViewController.m</p> <pre><code>- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; NSLog(@"Row clicked: %i", indexPath.row); if (_delegate != nil) { Product *product = (Product *) [_products objectAtIndex:indexPath.row]; [_delegate productSelectionChanged:product]; } DetailedVC *detailView = [[DetailedVC alloc] initWithNibName:@"DetailedVC" bundle:[NSBundle mainBundle]]; NSLog(@"User clicked on: %@", [NSString stringWithFormat:@"%d",indexPath.row]); detailView.detailDescriptionLabel.text = [NSString stringWithFormat:@"%d",indexPath.row]; [self.navigationController pushViewController:detailView animated:YES]; } </code></pre> <p>What happens here is I push the DetailedVC into the left side where my table view is and what I really want to do is see the row clicked updated in my detailed view on the right. I am able to see in the Log window that I clicked on a certain index so I know I am capturing the click event and getting a value.</p> <p>Inside my DetailedVC.m I have the following code to update this label.</p> <pre><code>- (void)viewDidLoad { [super viewDidLoad]; [self configureView]; } - (void) configureView { if (self.detailItem) { self.detailDescriptionLabel.text = [self.detailItem description]; NSLog(@"Item: %@", [self.detailItem description]); } } </code></pre> <p>If I edit the viewDidLoad I get a (null) for the [self.detailItem description]</p> <pre><code>- (void)viewDidLoad { [super viewDidLoad]; NSLog(@"Detail description label: %@", [self.detailItem description]); } </code></pre> <p>ProductViewController.h</p> <pre><code>@interface ProductViewController : UIViewController &lt;UITableViewDataSource, UITableViewDelegate&gt; { NSMutableArray *_products; UITableView *_productsTableView; id&lt;ProductSelectionDelegate&gt; _delegate; } @property (nonatomic, strong) IBOutlet UITableView *productsTableView; @property (nonatomic, retain) NSMutableArray *products; @property (nonatomic, assign) id&lt;ProductSelectionDelegate&gt; delegate; @end </code></pre> <p>AppDelegate.h</p> <pre><code>@class ProductViewController; @class DetailedVC; @interface TabAndSplitAppAppDelegate : NSObject &lt;UIApplicationDelegate, UITabBarControllerDelegate&gt; { UIWindow *window; UITabBarController *tabBarController; ProductViewController *_productViewController; DetailedVC *_detailedViewController; } @property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet UITabBarController *tabBarController; @property (nonatomic, retain) IBOutlet ProductViewController *productViewController; @property (nonatomic, retain) IBOutlet DetailedVC *detailedViewController; @end </code></pre> <p>AppDelegate.m</p> <pre><code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSMutableArray *products = [NSMutableArray array]; [products addObject:[[Product alloc] initWithName:@"Product 1 Name" desc:@"Product 1 Description"]]; _detailedViewController.product = [products objectAtIndex:0]; // Override point for customization after app launch. //create split view controller RootVC *rvc=[[RootVC alloc] init]; rvc.title=@"Root VC"; DetailedVC *dvc=[[DetailedVC alloc] init]; dvc.title=@"Detailed VC"; _productViewController.delegate = _detailedViewController; MySplitViewController *msc = [[MySplitViewController alloc] initwithLeftVC:rvc rightVC:dvc]; msc.title=@"First"; //create a temporary VC to show in second tab SecondViewController *vc2 = [[SecondViewController alloc] init]; vc2.title=@"Second"; //make an array containing these two view controllers NSArray *viewControllers = [NSArray arrayWithObjects:msc,vc2,nil]; [tabBarController setViewControllers:viewControllers]; tabBarController.view.backgroundColor=[UIColor blackColor]; for(UITabBarItem*t in tabBarController.tabBar.items) { t.image=[UIImage imageNamed:@"icon.png"]; t.badgeValue=[NSString stringWithFormat:@"%d",([tabBarController.tabBar.items indexOfObject:t]+1)]; } //the views are retained their new owners, so we can release [rvc release]; [dvc release]; [msc release]; [vc2 release]; // Add the tab bar controller's current view as a subview of the window [self.window addSubview:tabBarController.view]; [self.window makeKeyAndVisible]; return YES; } </code></pre> <p>MySplitViewController.h</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @interface MySplitViewController : UIViewController { UINavigationController *leftController; UINavigationController *rightController; } @property (nonatomic, retain) UINavigationController *leftController; @property (nonatomic, retain) UINavigationController *rightController; - (void)layoutViews:(UIInterfaceOrientation)orientation initialVerticalOffset:(float)offset; - (MySplitViewController*) initwithLeftVC:(UIViewController*)leftvc rightVC:(UIViewController*)rightvc; @end </code></pre> <p>MySplitViewController.m</p> <pre><code>- (MySplitViewController*) initwithLeftVC:(UIViewController*)leftvc rightVC:(UIViewController*)rightvc { if(self=[super init]) { UINavigationController *lnc=[[UINavigationController alloc] initWithRootViewController:leftvc]; lnc.navigationBarHidden=NO; self.leftController=lnc; [lnc release]; UINavigationController *rnc=[[UINavigationController alloc] initWithRootViewController:rightvc]; rnc.navigationBarHidden=NO; self.rightController=rnc; [rnc release]; } return self; } </code></pre> <p>Product.h</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; @interface Product : NSObject { NSString *_productID; NSString *_productDescription; } @property (nonatomic, copy) NSString *productID; @property (nonatomic, copy) NSString *productDescription; - (Product *)initWithName:(NSString *)productID desc:(NSString *)productDescription; @end </code></pre> <p>Product.m</p> <pre><code>#import "Product.h" @implementation Product @synthesize productID = _productID; @synthesize productDescription = _productDescription; - (Product *)initWithName:(NSString *)productID desc:(NSString *)productDescription { if ((self = [super init])) { self.productID = productID; self.productDescription = productDescription; } return self; } @end </code></pre>
    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.
 

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