سؤال

I am new in iOS programming.

What I am trying to do is:

I have some views in a storyboard and I'd like to switch between the views programatically. For example, when a button is clicked, I call a method and I want to change views here (I can call the method successfully). The buttons are also created programatically in different positions.

I have searched and I think it happens with NavigationController. I have a navigation controller which I created like so: menu Editor -> Embed In -> NavigationController. How could I do this using this NavigationController?

@Madhu and @Dilip ,I found a solution with xib filed class

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];

I have a class with xib file named icerik, I solved it like this. It is opening but when I want to turn back, What can I do it ?

هل كانت مفيدة؟

المحلول

You can create btn using this code:

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];

and for going to another vc use this code,if you want navigation bar:

-(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];
}

Else use this code:

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

    [self presentModalViewController:secondViewController            animated:YES];
}

And for come back to frist vc fromm second vc add this code in second 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];
}

نصائح أخرى

If your new to Objective-c first go with Views/ViewControllers. i.e. use addSubView property of UIView

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

If your little known of UINavigationCOntroller Use pushViewController

CCFilteredVideosController *filteredController = [[CCFilteredVideosController alloc] initWithNibName:@"CCFilteredVideosController" bundle:nil];
[self.navigationController pushViewController:filteredController animated:YES];
[filteredController release];
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top