Navigating to another view controller without a na

2019-04-01 14:48发布

As you guess am still a newbie, getting my head around iphone development. I am just trying out basic view loading on demand, which i cant get to work

I have an app, with 2 view controllers, each view controller connected a different nib file. I am trying to switch between view manually; there is no navigation control involved.

How can i manually push the second view to the first view? self.navigationController pushViewController wont work since there is no navigation controller. How else can I push the second view on top of the first view and destroy the first view; and ofcourse vice versa? I have done this in the first view's button action:

SecondView *sv=[[SecondView alloc] initWithNibName:@"SecondView" bundle:nil];
[self.navigationController pushViewController:sv animated:YES];

obviously, it didn't work.

window addSubView didn't work either, because the first view controller is the root view controller (not sure i said that right). In other words, when i run the app, the first view is what I see with a button that is supposed to load the second view.

I have spent hours searching for a simple example, and I couldn't find any.

Any suggestions?

标签: iphone views
3条回答
一纸荒年 Trace。
2楼-- · 2019-04-01 15:09

try :

SecondView *sv=[[SecondView alloc] initWithNibName:@"SecondView" bundle:nil];
[self presentModalViewController:sv animated:YES];
查看更多
甜甜的少女心
3楼-- · 2019-04-01 15:12

in the first view controller you need this:

- (IBAction)pushWithoutViewController:(id)selector {
    NextNavigationController *page = [[NextNavigationController alloc] initWithNibName:NextNavigationController bundle:nil];
    CGRect theFrame = page.view.frame;
    theFrame.origin = CGPointMake(self.view.frame.size.width, 0);
    page.view.frame = theFrame;
    theFrame.origin = CGPointMake(0,0);
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.8f];
    page.view.frame = theFrame;
    [UIView commitAnimations];
    [self.view addSubview:page.view];
    [page release];
}

and then link it to the button in nib. :)

查看更多
4楼-- · 2019-04-01 15:30

IF you have first xib and you want to give navigation to another controller then you have to declare navigation in to appdelegate.m write following code to app

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navController;

then in ViewController.m

- (IBAction)NextButtonClicked:(id)sender
{
    yourNextViewController *objyourNextViewController = [[yourNextViewController alloc] initWithNibName:@"yourNextViewController" bundle:nil];
    [self.navigationController pushViewController:objStartUpViewController animated:TRUE];
}
查看更多
登录 后发表回答