我正在尝试在导航控制器上创建一个按钮,该按钮会导致转换到另一个视图控制器(具有前一个导航视图的后退按钮)。 我正在尝试以编程方式执行此操作。 我制作了一个UINavigationController和一个UITabBarController。导航控制器是选项卡控制器的打开选项卡。 我创建了一个名为SubVieController的UIViewController的子类。我没有向Sub类添加任何内容,只添加了自动生成的材质。 我已经对Appdelegate.m中的didFInishLaunchingWithOptions方法进行了所有编辑。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
UITabBarController * tabBarController = [[UITabBarController alloc] init];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
SubViewController * firstTab= [[SubViewController alloc] initWithNibName:@"SubViewController" bundle:nil];
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:firstTab];
SecondViewController *secondTab = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
SubTableController *table = [[SubTableController alloc]initWithNibName:@"SubTableController" bundle:nil];
navigationController1.tabBarItem.title = @"First Nav";
secondTab.tabBarItem.title =@"Second";
table.title = @"Third Table";
tabBarController.viewControllers = @[navigationController1,secondTab, table];
[self.window setRootViewController:tabBarController];
[self.window makeKeyAndVisible];
return YES;
}
我尝试将一个Button移动到SubViewController.xib文件中以将其连接到代码中,以便我可以进行按钮操作。但是当我按住Ctrl +拖动时,没有任何东西像往常一样插入到代码中。
如何让按钮导致从起始视图转换到仍被视为位于第一个选项卡上的单独视图,并且第一个视图的后退按钮也位于第一个选项卡中?< / p>
编辑: 此代码似乎适用于按钮操作: - (IBAction)转换:(UIButton *)sender { SubViewController * view2 = [[SubViewController alloc] init]; [self.navigationController pushViewController:view2 animated:YES]; }
答案 0 :(得分:0)
正如您所做的那样,UITabBarController
不应推送UIViewController
而应推送UINavigationController
。 UINavigationController
应该有一个rootViewController
。
无法在Interface Builder中进行Control-Drag通常意味着Identity Inspector中的Custom Class设置不正确。
@implementation SubViewController
:<强>的OBJ-C 强>
UIViewController
<强>夫特强>
UIViewController * vc = [[UIViewController alloc] initWithNibName:@"id" bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
架构建议: 重新开始使用Storyboard
使用Storyboard无需编写单行代码来实现上述解决方案!