Iphone 点击tableview单元格上的新视图控制器
我有一个头文件是Iphone 点击tableview单元格上的新视图控制器,iphone,uiviewcontroller,ios6,uitableview,didselectrowatindexpath,Iphone,Uiviewcontroller,Ios6,Uitableview,Didselectrowatindexpath,我有一个头文件是 @interface DemoFirstViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> @end 另一个ViewController文件是 @interface AnotherViewController : UIViewController { IBOutlet UILabel *message; } @property (nonatomic
@interface DemoFirstViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@end
另一个ViewController文件是
@interface AnotherViewController : UIViewController
{
IBOutlet UILabel *message;
}
@property (nonatomic , retain) UILabel *message;
@end
我使用Xib文件来完成这一切。不是故事板
这是基于选项卡的应用程序。和两个viewcontroller已在Appdelegate中声明
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController1 = [[DemoFirstViewController alloc] initWithNibName:@"DemoFirstViewController" bundle:nil];
UIViewController *viewController2 = [[DemoSecondViewController alloc] initWithNibName:@"DemoSecondViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = @[viewController1, viewController2];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
在表单元格点击时,另一个视图控制器未启动。请尽快回复
NSLog(@"Navigation Controller: %@", self.navigationController);
检查这行打印的内容。
我怀疑您忘了在层次结构中添加navigationController
更新:
根据您的AppDelegate代码,按如下方式进行更新以解决您的问题:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController1 = [[DemoFirstViewController alloc] initWithNibName:@"DemoFirstViewController" bundle:nil];
// here I create a Navigation Controller and set its root view controller to viewController1
UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
UIViewController *viewController2 = [[DemoSecondViewController alloc] initWithNibName:@"DemoSecondViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
// updated this line to show the navController1 (which contains viewController1)
self.tabBarController.viewControllers = @[navController1, viewController2];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
如果连viewController2都是需要推送内容的UITableViewController,请在其上执行相同的操作(添加另一个UINavigationController,设置根viewcontroller,并设置TabBarController的一个viewcontroller)
检查这行打印的内容。
我怀疑您忘了在层次结构中添加navigationController
更新:
根据您的AppDelegate代码,按如下方式进行更新以解决您的问题:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController1 = [[DemoFirstViewController alloc] initWithNibName:@"DemoFirstViewController" bundle:nil];
// here I create a Navigation Controller and set its root view controller to viewController1
UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
UIViewController *viewController2 = [[DemoSecondViewController alloc] initWithNibName:@"DemoSecondViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
// updated this line to show the navController1 (which contains viewController1)
self.tabBarController.viewControllers = @[navController1, viewController2];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
如果viewController2是一个UITableViewController,需要推送某些内容,则在其上执行相同的操作(添加另一个UINavigationController,设置根viewcontroller,并设置TabBarController的一个viewcontroller)这背后可能有一些原因:
UINavigationController
应在Appdelegate
类中正确实现UITableView
不应添加到self.view
的任何子视图上
didSelectRowAtIndexPath:
,那么您忘记设置tableView
delegate也没关系tableView.delegate = self;
选项卡
,他之前没有提到,所以我正在编辑我的答案:-
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
DemoFirstViewController *viewController1 = [[DemoFirstViewController alloc] initWithNibName:@"DemoFirstViewController" bundle:nil];
UINavigationController *nav1 =[[UINavigationController alloc] initWithRootViewController:viewController1];
DemoSecondViewController *viewController2 = [[DemoSecondViewController alloc] initWithNibName:@"DemoSecondViewController" bundle:nil];
UINavigationController *nav2 =[[UINavigationController alloc] initWithRootViewController:viewController2];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = @[nav1, nav2];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
这背后可能有几个原因:
UINavigationController
应在Appdelegate
类中正确实现UITableView
不应添加到self.view
的任何子视图上
didSelectRowAtIndexPath:
,那么您忘记设置tableView
delegate也没关系tableView.delegate = self;
选项卡
,他之前没有提到,所以我正在编辑我的答案:-
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
DemoFirstViewController *viewController1 = [[DemoFirstViewController alloc] initWithNibName:@"DemoFirstViewController" bundle:nil];
UINavigationController *nav1 =[[UINavigationController alloc] initWithRootViewController:viewController1];
DemoSecondViewController *viewController2 = [[DemoSecondViewController alloc] initWithNibName:@"DemoSecondViewController" bundle:nil];
UINavigationController *nav2 =[[UINavigationController alloc] initWithRootViewController:viewController2];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = @[nav1, nav2];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
以上所有答案都指向正确的方向。万一您还没有找到它,请检查您是否已在
appdelegate
中添加了以下代码
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navigationController;
然后再次检查。以上所有答案都指向正确的方向。万一您还没有找到它,请检查您是否已在
appdelegate
中添加了以下代码
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navigationController;
然后再次检查。didSelectRowAtIndexPath的NSLog是否正常工作?是否在
AppDelegate
中正确设置了DemoFirstViewController
与Navigation
控制器的initWithRootViewController
方法??或者您是否在另一个视图中实现了initWithNibName
方法?@Zeel-现在查看我编辑的答案~~您的didSelectRowAtIndexPath的NSLog是否正常工作?您是否在中正确设置了DemoFirstViewController
的导航控制器的initWithRootViewController
方法AppDelegate
??或者您是否在另一个视图中实现了initWithNibName
方法
?@Zeel-现在查看我编辑的答案~~+1。这几乎总结了所有新手在从tableview推视图控制器时犯的错误。我不理解第二点。如果使用UIViewController而不是UITableViewController(如本例中所示),则AFAIK UITableView可以是主视图的子视图,而没有任何风险@LombaX-UITableView可以是主视图的子视图
是的,我同意,但不应该是视图的子视图
,它也是主视图的子视图。。。。。我的意思是tableView
应该是添加到self.View
。嗯……好吧,我不知道:-)我会搜索,因为它看起来很“有趣”:-)如果你有一些指示/链接解释我为什么会心存感激!我之所以有这样的经历,是因为当我试图推动didSelectRow的观点时:我得到了这个理由,我希望其他人不要把时间浪费在同一个理由上:)+1。这几乎总结了所有新手在从tableview推视图控制器时犯的错误。我不理解第二点。如果使用UIViewController而不是UITableViewController(如本例中所示),则AFAIK UITableView可以是主视图的子视图,而没有任何风险@LombaX-UITableView可以是主视图的子视图
是的,我同意,但不应该是视图的子视图
,它也是主视图的子视图。。。。。我的意思是tableView
应该是添加到self.View
。嗯……好吧,我不知道:-)我会搜索,因为它看起来很“有趣”:-)如果你有一些指示/链接解释我为什么会心存感激!我之所以有这样的经验,是因为当我试图通过didSelectRow推送视图时:我得到了这个原因,我希望其他人不要把时间浪费在同一个原因上:)意味着另一个ViewController应该是UINavigationController的子类?在这个文件的xib中,我必须从对象中拖放navigationcontroller?不,您只需将导航控制器添加到控制器层次结构中。我看到您添加了AppDelegate代码,我更新了我的答案,并向您展示了要更改的内容。感谢所有人的回复。但当我在appDelegate上添加上述代码后按下表格单元格时,我的应用程序将终止,出现以下异常….>>>>>TabBarNavBarDemo[799:c07]***由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:'-[UIViewCont