0

我是 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 文件的类,我像这样解决了它。它正在开放,但是当我想回头时,我该怎么办?

4

2 回答 2

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];
于 2013-02-19T12:30:18.063 回答
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];
}

为了从第二个 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];
}
于 2013-02-19T12:31:33.613 回答