xcode pushViewController 不适用于标签栏控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8544963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pushViewController not working with tab bar controller
提问by user973985
I am trying to push in a new view controller with the code:
我正在尝试使用以下代码推送一个新的视图控制器:
[self.navigationController pushViewController:webViewController animated:YES];
But its not working. This code happens when you select a cell in my table view. I have a table view which I think is preventing this from happening. I have tried presenting it modally but that gets rid of the navigation bar and i can't go back. There is an extremely similar to this but the answer didn't work for me!
但它不工作。当您在我的表格视图中选择一个单元格时会发生此代码。我有一个表格视图,我认为它可以防止这种情况发生。我曾尝试以模态方式呈现它,但它摆脱了导航栏,我无法返回。有一个非常相似的,但答案对我不起作用!
UPDATE
更新
- (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];
}
puts the web view as the WebViewController's view
将 web 视图作为 WebViewController 的视图
there is no nib as shown above
没有如上所示的笔尖
the didSelect cell method IS CALLED using an NSLog to find out
使用 NSLog 调用 didSelect 单元格方法来找出
Everything is initialized and allocated and non-zero
一切都被初始化和分配并且非零
UPDATE:
更新:
my did select cell method:
我确实选择了单元格方法:
-(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]];
}
ALL OF THIS WORKED BEFORE INSERTING TAB BAR CONTROLLER
所有这些在插入标签栏控制器之前都有效
UPDATE
更新
Where I create the controllers (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;
}
So I really have no idea whats going on here. Also IF YOU COULD PLEASE HELP ME WITH ANOTHER QUESTION! Go to my profile and see the question about how to stop cutoffs/add extra line for text label in uitableviewcell
所以我真的不知道这里发生了什么。另外,如果你能帮我解决另一个问题!转到我的个人资料并查看有关如何在 uitableviewcell 中停止截断/为文本标签添加额外行的问题
回答by Ken W
I don't see the initial UINavigationViewController in your app delegate. In order to use the self.navigationController pushViewController, the view controller needs to be in one of the self.navigationController.viewControllers. Can you modify your code and try the following and see it works for you.
我在您的应用程序委托中没有看到初始 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];
回答by ODB
If the view controller is under the "More" button, then things change a bit.
如果视图控制器位于“更多”按钮下,则情况会有所改变。
Here's what worked for me to figure out what navigation controller to use:
以下是我找出要使用的导航控制器的方法:
- (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];
}
Basically if the user is on a view under the More tab, the actual current UINavigationController you want to use to push a UIViewController onto is the UITabBarController's moreNavigationController property. If on a view actually visible on the tab bar itself, use the UITabBarController's selectedViewController property. NOTE: if you're on the "More screen" itself, you can use either property.
基本上,如果用户在 More 选项卡下的视图上,则要用于将 UIViewController 推送到的实际当前 UINavigationController 是 UITabBarController 的 moreNavigationController 属性。如果在选项卡栏本身上实际可见的视图上,请使用 UITabBarController 的 selectedViewController 属性。注意:如果您在“更多屏幕”本身,则可以使用任一属性。
回答by user973985
There are several points you have to check, in order to make the push successful.
为了使推送成功,您必须检查几点。
First, the view controller to be pushed is probably designed and stored in a NIB file. Check how you create it in your code, particularly the NIB file name (assuming you use -(id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle
). It must be the real NIB filename without the NIB/XIB extension.
首先,要推送的视图控制器可能设计并存储在 NIB 文件中。检查您如何在代码中创建它,尤其是 NIB 文件名(假设您使用-(id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle
)。它必须是没有 NIB/XIB 扩展名的真实 NIB 文件名。
Second, is the selection event triggered and the message really sent to the table view delegate ? You could put some logging to ensure that. The table view won't prevent the view controller from being pushed on the navigation stack.
其次,是否触发了选择事件并将消息真正发送到表视图委托?您可以进行一些日志记录以确保这一点。表视图不会阻止视图控制器被推送到导航堆栈上。
The question isn't really precise on how/where you create that controller, so finally, if none of this works, make the test to alloc/init the controller just before pushing. You might have a better understanding of what is going on.
关于如何/在哪里创建该控制器的问题并不是很准确,所以最后,如果这些都不起作用,请在推送之前进行测试以分配/初始化控制器。您可能对正在发生的事情有更好的了解。
回答by Sarah
Did you do it in the manner like below??
你是按照下面的方式做的吗??
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(reqList==Nil)
{
reqList = [[RequestList alloc]initWithNibName:@"RequestList" bundle:nil];
}
[self.navigationController pushViewController:reqList animated:YES];
}