为什么要创建该窗口对象,为什么要尝试将子视图添加到其中?如果要添加子视图,则应将其添加到父视图、tableview 或 tableView 的父视图。
一个更好的主意是在堆栈上推送一个新的视图控制器,以显示您想要显示的信息。
这是一个教程,展示了如何在 tableview教程链接中选择单元格时推送新的视图控制器。
编辑:在 MultipleAppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 应该如下所示:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[MultipleViewController alloc] initWithNibName:@"MultipleViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
navController.navigationBarHidden = YES;
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
在 GadgetBulletinsTVContoller.h 中声明如下协议:
@protocol GadgetBulletinsTVControllerDelegate <NSObject>
@optional
- (void)showItemDetails:(id)selectedItem;
@end
和一个委托属性:
@property (nonatomic, assign)id<GadgetBulletinsTVControllerDelegate>delegate;
在 GadgetBulletinsTVContoller.m 中合成委托。- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 应该是这样的:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if([delegate respondsToSelector:@selector(showItemDetails:)])
{
[delegate showItemDetails:[subject_data_Array objectAtIndex:indexPath.row]];
}
}
在 FirstViewController.m 中告诉控制器像这样实现 GadgetBulletinsTVControllerDelegate:
@interface FirstViewController ()<GadgetBulletinsTVControllerDelegate>
in viewDidLoad method tell the gadgetBulletinsController that his delegate is the FirstViewController class, like this:
if (gadgetBulletinsContoller == nil) {
gadgetBulletinsContoller = [[GadgetBulletinsTVContoller alloc] init];
gadgetBulletinsContoller.delegate = self;
}
并实现 GadgetBulletinsTVControllerDelegate 的方法:
- (void)showItemDetails:(id)selectedItem
{
if([delegate respondsToSelector:@selector(showDetailsScreenForItem:)])
{
[delegate showDetailsScreenForItem:selectedItem];
}
}
在 FirstViewController.h 中声明如下协议:
@protocol FirstViewControllerDelegate <NSObject>
- (void)showDetailsScreenForItem:(id)item;
@end
并声明如下委托属性(不要忘记在 .m 文件中合成):
@property (nonatomic, assign)IBOutlet id<FirstViewControllerDelegate>delegate;
在 MultipleViewController.xib 中选择 FirstViewController 屏幕并在 outlets 中从委托拖动到 fileOwner 以将委托的值设置为 MultipleViewController(如果您愿意,可以在代码中执行此操作)。
在 MultipleViewController.m 中告诉 MultipleViewController 实现 FirstViewControllerDelegate 协议,如下所示:
@interface MultipleViewController ()<FirstViewControllerDelegate>
并实现协议方法:
- (void)showDetailsScreenForItem:(id)item
{
DetailViewController *detailController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:nil];
[detailController changeSubjectText:item];
[self.navigationController pushViewController:detailController animated:YES];
}
在 DetailViewController 中修改 closeDetail 方法,如下所示:
- (IBAction)closeDetail:(id)sender {
NSLog(@"closeDetail");
[self.navigationController popViewControllerAnimated:YES];
}
瞧,您的 GadgetBulletinsTVController 项目详细信息被推送。您需要对要显示详细信息的其他控制器执行相同的步骤。