UITableView [self.navigationController pushViewController:Page1 animated:YES];方法不起作用

时间:2012-10-26 09:51:06

标签: iphone uitableview pushviewcontroller addsubview

这是我的方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath    *)indexPath
{
if (indexPath.row == 0 && indexPath.section == 0){
    [self.navigationController pushViewController:Page1 animated:YES];
}
if (indexPath.row == 1 && indexPath.section == 0){
    [self.view addSubview:Page2.view];  
}

我改变了方法

[self.view addSubview:Page1.view]; //this works perfectly however as soon as I change to this
[self.navigationController pushViewController:Page1 animated:YES];// this does not work

正在调用该方法,我已经在之前和之后添加了NSLog,并且两者都被调用了。但是,调用此方法时只会使单元格变为蓝色(但这会发生在addSubview和pushViewController方法中) 我推动的观点只是一个普通的UIView。 我可以添加我需要的视图,但它们不是动画的(因为我使用的是addSubview方法)

感谢您的帮助:)

4 个答案:

答案 0 :(得分:1)

我猜您没有正确设置导航控制器,您可以在appdelegate.m文件中的didFinishLaunchingWithOptions方法中实现此功能

self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];

self.navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];

[self.navController.navigationBar setBarStyle:UIBarStyleBlack];

[self.window addSubview:self.navController.view];

答案 1 :(得分:0)

最好将navController设为rootViewController的{​​{1}}:

window

答案 2 :(得分:0)

因为,用户编辑了问题,所以我再次给出答案。如果你想使用NavigationController的tabBar

IN AppDelegate.h定义UINavigationController *navController;并创建属性并在appDelegate.m中合成它。

导入AppDelegate.m中的所有ViewController类

喜欢

#import "ViewController.h"
#import "Preview.h"
#import "Export.h"
#import "Settings.h"
#import "More.h"



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.


tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate=self;

//Adding Search,Nearby,Map,AboutUs,Favorites Tabs to tabBarController
ViewController * search = [[ViewController alloc] init];
UINavigationController *searchNav = [[UINavigationController alloc]        initWithRootViewController:search];

Preview* nearby = [[Preview alloc] init];
UINavigationController *nearbyNav = [[UINavigationController alloc] initWithRootViewController:nearby];

Export* map = [[Export alloc] init];
UINavigationController *mapNav = [[UINavigationController alloc] initWithRootViewController:map];

Settings* aboutUs = [[Settings alloc] init];
UINavigationController *aboutUsNav = [[UINavigationController alloc] initWithRootViewController:aboutUs];

More* favorites = [[More alloc] init];
UINavigationController *favoritesNav = [[UINavigationController alloc] initWithRootViewController:favorites];

NSArray* controllers = [NSArray arrayWithObjects:searchNav,nearbyNav,mapNav,aboutUsNav,favoritesNav, nil];

tabBarController.viewControllers = controllers;

[self.window addSubview:tabBarController.view];

//  self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
//  self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;

}

可能会帮助你。

答案 3 :(得分:0)

viewController page1当你添加到self.view时,如何在推送时获得动画,因为它已经位于视图顶部

[self.view addSubview:Page1.view]; //this works perfectly however as soon as I change to this
[self.navigationController pushViewController:Page1 animated:YES];// this does not work

希望有所帮助