obj c,xcode进行视图转换

时间:2015-07-10 00:59:55

标签: ios

我正在尝试在导航控制器上创建一个按钮,该按钮会导致转换到另一个视图控制器(具有前一个导航视图的后退按钮)。 我正在尝试以编程方式执行此操作。 我制作了一个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]; }

1 个答案:

答案 0 :(得分:0)

正如您所做的那样,UITabBarController不应推送UIViewController而应推送UINavigationControllerUINavigationController应该有一个rootViewController

  

无法在Interface Builder中进行Control-Drag通常意味着Identity Inspector中的Custom Class设置不正确。

连接IBAction

  1. 在Xcode项目中,选择SubViewController.xib
  2. 选择按钮
  3. Xcode&gt;查看&gt;公用事业&gt;显示Connections Inspector
  4. Xcode&gt;查看&gt;助理编辑&gt;显示助理编辑器
  5. 确保自动&gt; SubViewController.m&gt;没有选择。如果没有,您可能没有在IB
  6. 中正确设置自定义类
  7. 在“连接”中,点击触摸内部索环,然后将其拖至“{1}}
  8. 下方的智能助理”
  9. 输入名称,例如 buttonTappedAction
  10. 根据需要添加代码以推送@implementation SubViewController
  11. <强>的OBJ-C

    UIViewController

    <强>夫特

    UIViewController * vc = [[UIViewController alloc] initWithNibName:@"id" bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
    

    故事板

      

    架构建议:   重新开始使用Storyboard

    使用Storyboard无需编写单行代码来实现上述解决方案!

    enter image description here