我正在尝试使用代码推送新的视图控制器:
[self.navigationController pushViewController:webViewController animated:YES];
但它不起作用。在表视图中选择单元格时会发生此代码。我有一个表视图,我认为这是防止这种情况发生的。我试过以模态方式呈现它,但是摆脱了导航栏,我无法回头。这与此非常相似,但答案对我不起作用!
更新
- (void)loadView {
// Create an instance of UIWebView as large as the screen
CGRect screenFrame = [[UIScreen mainScreen] applicationFrame];
UIWebView *wv = [[UIWebView alloc] initWithFrame:screenFrame];
// Tell web view to scale web content to fit within bounds of webview
[wv setScalesPageToFit:YES];
[self setView:wv];
[wv release];
}
将Web视图作为WebViewController的视图
如上所示没有笔尖
使用NSLog查找didSelect单元格方法以查找
所有内容都已初始化并已分配且非零
更新:
我选择了细胞方法:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Push the web view controller onto the navigaton stack - this implicity
// creates the web view controller's view the first time through
NSLog(@"TOUCHED!");
webViewController = [[WebViewController alloc] init];
if (webViewController == nil) {
NSLog(@"webViewController in nil state");
}
[self.navigationController pushViewController:webViewController animated:YES];
// Grab the selected item
RSSItem *entry = [[channel items] objectAtIndex:[indexPath row]];
// Construct a URL with the link string of the item
NSURL *url = [NSURL URLWithString:[entry link]];
// Construct a request object with that URL
NSURLRequest *req = [NSURLRequest requestWithURL:url];
// Load the request into the web view
[[webViewController webView] loadRequest:req];
// Set the title of the web view controller's navigation item
[[webViewController navigationItem] setTitle:[entry title]];
}
在插入TAB BAR控制器之前所有这些工作
更新
我在哪里创建控制器(IN APP DELEGATE):
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
ListViewController *lvc = [[ListViewController alloc] initWithStyle:UITableViewStylePlain];
[lvc autorelease];
// Create the tabBarController
UITabBarController *tabBarController = [[UITabBarController alloc] init];
// Create two view controllers
UIViewController *vc1 = [[ListViewController alloc] initWithStyle:UITableViewStyleGrouped];
UIViewController *vc2 = [[YoutubeViewController alloc] init];
// Make an array containing the two view controllers
NSArray *viewControllers = [NSArray arrayWithObjects:vc1, vc2, nil];
// The viewControllers array retains vc1 and vc2, we can release
// our ownership of them in this method
[vc1 release];
[vc2 release];
// Attach them to the tab bar controller
[tabBarController setViewControllers:viewControllers];
// Put the tabBarController's view on the window
[[self window] setRootViewController:tabBarController];
// The window retains tabBarController, we can release our reference
[tabBarController release];
// Show the window
[[self window] makeKeyAndVisible];
return YES;
}
所以我真的不知道这里发生了什么。如果您可以帮我解决另一个问题!转到我的个人资料,看看有关如何在uitableviewcell中停止截止/为文字标签添加额外行的问题
答案 0 :(得分:5)
我没有在你的app委托中看到最初的UINavigationViewController。为了使用self.navigationController pushViewController,视图控制器需要位于self.navigationController.viewControllers中。您可以修改您的代码并尝试以下内容,看它是否适合您。
// Create two view controllers
UIViewController *vc1 = [[ListViewController alloc] initWithStyle:UITableViewStyleGrouped];
UIViewController *vc2 = [[YoutubeViewController alloc] init];
// Create the UINavigationController and put the list view controller as the root view controller
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:vc1];
// Make an array containing the two view controllers and the UINavigationController which has the ListViewController is the first one.
NSArray *viewControllers = [NSArray arrayWithObjects:navController, vc2, nil];
// The viewControllers array retains vc1 and vc2, we can release
// our ownership of them in this method
[vc1 release];
[vc2 release];
[navController release];
// Attach them to the tab bar controller
[tabBarController setViewControllers:viewControllers];
答案 1 :(得分:2)
为了使推送成功,您必须检查几点。
首先,要推送的视图控制器可能是设计并存储在NIB文件中。检查如何在代码中创建它,尤其是NIB文件名(假设您使用-(id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle
)。它必须是没有NIB / XIB扩展名的真实NIB文件名。
其次,触发的选择事件是否真的发送到表视图委托?您可以进行一些日志记录以确保这一点。表视图不会阻止视图控制器被推送到导航堆栈。
关于如何/在何处创建该控制器的问题并非如此精确,所以最后,如果这些都不起作用,请在推送之前进行测试以分配/初始化控制器。您可能对正在发生的事情有了更好的了解。
答案 2 :(得分:2)
如果视图控制器位于“更多”按钮下,则情况会发生一些变化。
这对我有用,可以找出要使用的导航控制器:
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
UITabBarController *tabBar = (UITabBarController*) self.window.rootViewController;
UINavigationController* navigationController = (UINavigationController*)self.tabBarController.selectedViewController;
UIViewController* top = nil;
if ([navigationController respondsToSelector:@selector(topViewController)]) {
top = navigationController.topViewController;
}
// the actual current navigation controller depends on whether we are on a view under the More tab
UINavigationController *curNavController;
if (top == nil) { // this implies a view under the More tab
curNavController = tabBar.moreNavigationController;
} else {
curNavController = navigationController;
}
[[LocalNotificationMgr Get] ReceivedNotification:notif navController:curNavController];
}
基本上,如果用户位于“更多”选项卡下的视图中,则要用于将UIViewController推送到的实际当前UINavigationController是UITabBarController的moreNavigationController属性。 如果在标签栏本身上实际可见的视图上,请使用UITabBarController的selectedViewController属性。 注意:如果您使用“更多屏幕”本身,则可以使用任一属性。
答案 3 :(得分:1)
你是否以下面这样的方式做到了?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(reqList==Nil)
{
reqList = [[RequestList alloc]initWithNibName:@"RequestList" bundle:nil];
}
[self.navigationController pushViewController:reqList animated:YES];
}