【问题标题】:Switch view to another view on storyboard将视图切换到情节提要上的另一个视图
【发布时间】:2013-02-19 12:21:47
【问题描述】:

我是 iOS 编程新手。

我想做的是:

我在情节提要中有一些视图,我想以编程方式在视图之间切换。比如,当一个按钮被点击时,我调用了一个方法,我想在这里改变视图(我可以成功调用该方法)。这些按钮也是以编程方式在不同位置创建的。

我已经搜索过,我认为 NavigationController 会发生这种情况。我有一个这样创建的导航控制器:菜单Editor -> Embed In -> NavigationController。我怎么能用这个NavigationController 做到这一点?

@Madhu 和@Dilip,我找到了xib 归档类的解决方案

icerik *secondViewController = [[icerik alloc] initWithNibName:@"icerik" bundle:nil];

    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController];
    navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
    navigationController.topViewController.title = @"SecondViewController";

    //[self presentModalViewController:navigationController  animated:YES];

    if([self respondsToSelector:@selector(presentViewController:animated:completion:)])
        [self presentViewController:navigationController animated:YES completion:^{/* done */}];
    else if([self respondsToSelector:@selector(presentViewController:animated:)])
        [self presentModalViewController:navigationController animated:YES];

我有一个名为icerik 的带有xib 文件的类,我是这样解决的。它正在开放,但当我想回头时,我该怎么办?

【问题讨论】:

  • 你想要这样,当用户点击 btn 时,他会从当前视图控制器转到另一个视图控制器,对吧?
  • 是的,我想使用情节提要更改两个视图控制器。

标签: ios uiview uinavigationcontroller views navigationcontroller


【解决方案1】:

您可以使用以下代码创建 btn:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(aMethod) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];

如果您想要导航栏,请使用此代码转到另一个 vc:

-(void)aMethod
{
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:SecondViewController];
    navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
    navigationController.topViewController.title = @"SecondViewController";

    [self presentModalViewController:navigationController               animated:YES];
}

否则使用此代码:

-(void)aMethod
{
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

    [self presentModalViewController:secondViewController            animated:YES];
}

然后从第二个 vc 回到第一个 vc,在第二个 vc 中添加此代码。

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(backAction:)]; 
    self.navigationItem.leftBarButtonItem = closeButton;
}
- (void)backAction:(id)sender {
    [self dismissModalViewControllerAnimated:NO];
}

【讨论】:

  • 我改了问题,你能再看看吗:)
【解决方案2】:

如果您是 Objective-c 的新手,请先使用 Views/ViewControllers。即使用UIViewaddSubView属性

UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, 250.0, kCCCellHeaderHeight)];
[subView setBackgroundColor:[UIColor redcolor]];
[self.view addSubview:subView];

如果您对UINavigationCOntroller 知之甚少,请使用pushViewController

CCFilteredVideosController *filteredController = [[CCFilteredVideosController alloc] initWithNibName:@"CCFilteredVideosController" bundle:nil];
[self.navigationController pushViewController:filteredController animated:YES];
[filteredController release];

【讨论】:

  • 我改了问题,请你再看一遍。:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-05
  • 1970-01-01
  • 2020-04-17
  • 1970-01-01
  • 2013-06-02
  • 2011-04-13
相关资源
最近更新 更多