Ios 正在从UITableView选定索引更新detailDescriptionLabel数据
我有一个用于iPad的拆分视图控制器,左侧有一个向下钻取表。我可以使用向下钻取来填充表,但我似乎无法使我在左侧的UITableView中单击的项显示在右侧的detailDescriptionLabel中 我的ProductViewController.m中有以下代码Ios 正在从UITableView选定索引更新detailDescriptionLabel数据,ios,uitableview,xib,Ios,Uitableview,Xib,我有一个用于iPad的拆分视图控制器,左侧有一个向下钻取表。我可以使用向下钻取来填充表,但我似乎无法使我在左侧的UITableView中单击的项显示在右侧的detailDescriptionLabel中 我的ProductViewController.m中有以下代码 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRow
- (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];
}
- (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;
}
这里发生的是我将DetailedVC推到我的表视图所在的左侧,我真正想做的是看到在右侧的detailed视图中单击的行被更新。我能够在日志窗口中看到我单击了某个索引,因此我知道我正在捕获单击事件并获得一个值
在我的DetailedVC.m中,我有以下代码来更新这个标签
- (void)viewDidLoad
{
[super viewDidLoad];
[self configureView];
}
- (void) configureView {
if (self.detailItem) {
self.detailDescriptionLabel.text = [self.detailItem description];
NSLog(@"Item: %@", [self.detailItem description]);
}
}
如果编辑viewDidLoad,则[self.detailItem description]会得到一个(null)
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"Detail description label: %@", [self.detailItem description]);
}
ProductViewController.h
@interface ProductViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
NSMutableArray *_products;
UITableView *_productsTableView;
id<ProductSelectionDelegate> _delegate;
}
@property (nonatomic, strong) IBOutlet UITableView *productsTableView;
@property (nonatomic, retain) NSMutableArray *products;
@property (nonatomic, assign) id<ProductSelectionDelegate> delegate;
@end
#import <UIKit/UIKit.h>
@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
MySplitViewController.h
@interface ProductViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
NSMutableArray *_products;
UITableView *_productsTableView;
id<ProductSelectionDelegate> _delegate;
}
@property (nonatomic, strong) IBOutlet UITableView *productsTableView;
@property (nonatomic, retain) NSMutableArray *products;
@property (nonatomic, assign) id<ProductSelectionDelegate> delegate;
@end
#import <UIKit/UIKit.h>
@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
产品.h
#import <Foundation/Foundation.h>
@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
好的,请在ProductViewController中尝试以下操作:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSLog(@"Row clicked: %i", indexPath.row);
Product *product = (Product *) [_products objectAtIndex:indexPath.row];
NSLog(@"Product: %@", product);
TabAndSplitAppAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSLog(@"appDelegate: %@", appDelegate);
UITabBarController *tbc = appDelegate.tabBarController;
NSLog(@"tbc: %@", tbc);
MySplitViewController *msvc = tbc.selectedViewController;
NSLog(@"msvc: %@", msvc);
UINavigationController *rnc = msvc.rightController;
NSLog(@"rnc: %@", rnc);
DetailedVC *dvc = rnc.topViewController;
NSLog(@"dvc: %@", dvc);
dvc.detailDescriptionLabel.text = @"We found our DetailedVC";
[dvc productSelectionChanged:product];
}
这段代码不是您应该如何正确构造程序的示例。这只是为了帮助我(和您)理解视图控制器层次结构
当选择产品时通知代表的最初想法是正确的,它在您的情况下不起作用,因为对象没有正确连接。不过你应该试着这么做。在您发布的代码中,我没有看到ProductViewController
和DetailedVC
都直接可见的位置,因此您可以说
productViewControllerInstance.delegate = detailedVCinstance;
最近的是AppDelegate,其中有
RootVC
实例rvc
,它可能最终会创建ProductViewController
和dvc
。也许您可以将dvc
交给rvc
,这样它可以在创建时将其设置为ProductViewController
的委托。祝你好运 您是否从configureView
获取NSLog输出?我看不到您在任何地方设置detailItem
。。。永远不会被执行。我的印象是这个值是在ProductViewController.mOk Jorn中的DidSelectRowatinex方法中设置的,现在我看到[self.detailItem description]显示了我在configureView方法中单击的行的值,它是:NSLog(@“Item:%@,[self.detailItem description]);这是一个很大的帮助,但我仍然没有看到该视图中的实际标签显示任何价值。我在DetailedVC.h文件中有一个名为detailDescriptionLabel的UILabel作为属性,并将其连接到接口生成器文件DetailedVC.xib中的实际UILabel-此标签在UI中有一个名为“label”的占位符值。运行应用程序时,如果看不到占位符值“Label”或更新的值。hm,可能是连接您的detailDescriptionLabel
outlet时出现问题。添加NSLog(@“Label:%@”,self.detailDescriptionLabel)时会得到什么结果代码>到配置视图
?我得到这个。。。。标签:哦,对不起,我显然是试图解决错误的问题。。。我以为你没有看到你在导航控制器上按下的细节视图控制器中的描述;但在拆分视图控制器的右侧细节视图中看不到它。。。好的,那么您的ProductViewController
委托的实例是哪个类?它的productSelectionChanged:
方法看起来如何?它被调用了吗?好的,我将ProductViewController.h文件添加到上面的线程中。我正在使用ProductViewController:UIViewController我的productSelectionChanged方法位于DetailedVC.m文件中,未被调用。
productViewControllerInstance.delegate = detailedVCinstance;