将视图切换到故事板上的另一个视图

时间:2013-02-19 12:21:48

标签: ios uiview uinavigationcontroller views navigationcontroller

我是iOS编程的新手。

我想做的是:

我在故事板中有一些观点,我想以编程方式在视图之间切换。例如,当单击一个按钮时,我调用一个方法,我想在这里更改视图(我可以成功调用该方法)。按钮也以编程方式在不同位置创建。

我已经搜索过,我认为它发生在NavigationController。我有一个我创建的导航控制器:menu 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文件类,我解决了这个问题。它正在打开,但当我想回头时,我该怎么做?

2 个答案:

答案 0 :(得分:0)

如果您的Objective-c新用户首先使用Views / ViewControllers。即使用addSubView

UIView属性
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];

答案 1 :(得分:0)

您可以使用以下代码创建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];
}

要回到frist vc fromm second 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];
}