ios 为什么 self.navigationController pushViewController 不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4221988/
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
Why won't self.navigationController pushViewController work?
提问by trio
I have a
我有一个
UIViewController-> UINavigationBar + UITableView
UIViewController-> UINavigationBar + UITableView
Just a bit more explanation
多一点解释
I made it through UIBuilder..
我是通过 UIBuilder 实现的..
1:Created a New UIViewController
with XIB-file
2:Using UIBuiler
i put a UINavigationController
3:Then i put UITableView
underneath the navigationBar
so it gave me..
1:UIViewController
用 XIB 文件创建一个新文件
2:使用UIBuiler
我放了一个UINavigationController
3:然后我把
它放在UITableView
下面,navigationBar
所以它给了我..
A: UIViewController-> UINavigationBar + UITableView
A: UIViewController-> UINavigationBar + UITableView
Now i am loading the data in UITableView from a Webservice which is working fine.
现在我正在从工作正常的 Web 服务加载 UITableView 中的数据。
I again made a xib with sam config which is
我再次使用 sam 配置制作了一个 xib,它是
B: UIViewController-> UINavigationBar + UITableView
B: UIViewController-> UINavigationBar + UITableView
So now when i try to push view B on view A using below code...it wont at all work...
所以现在当我尝试使用下面的代码在视图 A 上推送视图 B 时......它根本不起作用......
SelectSiteViewController *siteViewController = [[SelectSiteViewController alloc] initWithNibName:@"SelectSiteViewController" bundle:nil];
[self.navigationController pushViewController:siteViewController animated:YES];
When i checked the UINavigationController *nav = self.navigation
当我检查 UINavigationController *nav = self.navigation
nav is 0x0 that is i assume NIL.
nav 是 0x0,我假设 NIL。
Can anybody tell me whats wrong in here.. why is it nil...and how can i make it work..
任何人都可以告诉我这里出了什么问题..为什么它是零......我怎样才能让它工作..
Thanks a Lot....I would really appreciate any help
非常感谢....我真的很感激任何帮助
回答by jamihash
In UIBuilder verify that UINavigationController is referenced by the File's owner.
在 UIBuilder 中,验证文件的所有者是否引用了 UINavigationController。
回答by trio
Got it.
知道了。
I changed the Architecture a little bit.
我稍微改变了架构。
I made a New UIViewController class with its xib. And coded new UINavigationController.
我用它的 xib 创建了一个新的 UIViewController 类。并编写了新的 UINavigationController。
- (void)viewDidLoad {
[super viewDidLoad];
UINavigationController *navigationController
navigationController = [[UINavigationController alloc] init];
[self.view addSubview:navigationController.view];
switch (whichViewController) {
case 1:
viewController = [[xxxx alloc] init];
break;
case 2:
viewController = [[xxx1 alloc] init];
break;
default:
break;
}
[navigationController pushViewController:viewController animated:NO];
[viewController release];
}
}
And pushing the view in the Switch Statement....
并推动 Switch 语句中的视图....
I hope this makes sense ......
我希望这是有道理的 ......
Thanks jamihash
谢谢 jamihash
回答by Bourne
So you are adding the tableview to the navigation controller right? This is how:
所以你是将 tableview 添加到导航控制器吗?这是如何:
tableView = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
navigationController = [[UINavigationController alloc] init];
[navigationController pushViewController:tableView animated:NO];
The tableview gets added as the rootview to the navigation controller. And then on selecting a row if you wish to push another viewcontroller use the
tableview 作为 rootview 添加到导航控制器。然后在选择一行时,如果您希望推送另一个视图控制器,请使用
self.navigationController pushViewController: newViewController animated:YES];
inside the didSelectRowAtIndex method.
在 didSelectRowAtIndex 方法中。
NOTE: its UITableViewController *tableView and UINavigationController *navigationController by declaration. So code accordingly for your table.
注意:它的 UITableViewController *tableView 和 UINavigationController *navigationController 通过声明。所以相应地为你的桌子编码。
回答by sundsx
why UIViewController-> UINavigationBar + UITableView ?
为什么 UIViewController-> UINavigationBar + UITableView ?
I suggest you another approach ->UITableViewController A -> Embedded with a Navigation Controller, then after populate tableview you can pass data to ->UITableViewController B by [[self storyboard]instantiateViewControllerWithIdentifier:@"ControllerB"];
我建议你另一种方法 ->UITableViewController A -> 嵌入一个导航控制器,然后在填充 tableview 之后你可以通过 [[self storyboard]instantiateViewControllerWithIdentifier:@"ControllerB"] 将数据传递给 ->UITableViewController B;
then, in storyboard, drop a tableView B and in Identity->storyboard Id put some id.
然后,在故事板中,放置一个 tableView B 并在 Identity->storyboard Id 中放置一些 id。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *sUrl= @"<#string#>";
if (indexPath.row == 0) sUrl= @"http://www.apple.com/";
if (indexPath.row == 1) sUrl= @"http://www.google.com/";
if (indexPath.row == 2) sUrl= @"http://www.times.uk/";
NSMutableArray *selectedObject = [arrayOpenMale objectAtIndex:0];
NSLog(@"%@ is the selected object.",selectedObject);
SeriesAdetailVC *dvc = [[self storyboard]instantiateViewControllerWithIdentifier:@"DetailView"];
dvc.strings7 = [NSURL URLWithString:sUrl];
[self.navigationController pushViewController:dvc animated:YES];
}
hope help
希望帮助