2010-11-09 260 views
3

正如你猜测我还是一个新手,让我的脑袋围绕iphone开发。 我只是尝试了基本视图加载点播,我无法上班在没有导航控制器的情况下导航到另一个视图控制器

我有一个应用程序,2视图控制器,每个视图控制器连接不同的笔尖文件。 我试图手动切换视图;不涉及导航控制。

我该如何手动将第二个视图推送到第一个视图? self.navigationController pushViewController不会工作,因为没有导航控制器。 我还可以如何推动第一个视图上的第二个视图并销毁第一个视图;反之亦然? 我在第一种观点的按钮动作做到了这一点:显然

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

,它没有工作。

window addSubView也没有工作,因为第一个视图控制器是根视图控制器(不知道我说的是正确的)。换句话说,当我运行应用程序时,第一个视图是我看到的应该加载第二个视图的按钮。

我花了几个小时寻找一个简单的例子,我找不到任何。

有什么建议吗?

在第一视图控制器
+0

SecondView * sv = [[SecondVie w alloc] initWithNibName:@“SecondView”bundle:nil]; \t [self.view addSubview:sv.view]; \t [sv release];似乎工作,但没有动画;我如何添加动画就像它发生在导航控制器 – Veeru 2010-11-09 09:43:03

+0

编辑我的答案。 :)包括动画 – 2010-11-09 10:15:47

回答

10

你需要这个:

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

,然后将其在笔尖链接到该按钮。 :)

+0

伟大的,吉亚尼和托马斯,你的解决方案工作! Gyani的解决方案只给出一个动画,将其向上滑动,在presentModalViewController中是否有任何选项,我可以从右向左设置动画?托马斯的解决方案更精细,我可以控制动画以任何方式和速度我想要的。 – Veeru 2010-11-10 01:52:45

+0

此解决方案仅替换视图,而不是视图控制器。另外,initWithNibName接受第一个参数作为NSString,而不是class。因此,它应该被写为... initWithNibName:@“NextNavigationController”... – Raptor 2011-05-11 04:30:56

+0

啊,发现在initWithNibName前面...忘了我的@“”...:p你永远不会“替换”一个视图控制器。它只是一个对象。你所做的只是拥有物体,而不是将其交给导航控制器来控制。 – 2011-05-11 09:24:38

8

尝试:

SecondView *sv=[[SecondView alloc] initWithNibName:@"SecondView" bundle:nil]; 
[self presentModalViewController:sv animated:YES]; 
+0

嗨gyani,是否有可能用相同的语句添加不同类型的动画? – Veeru 2010-11-10 02:30:12

+2

@Veeru是的,可以添加不同类型的动画 例如 SecondView * sv = [[SecondView alloc] initWithNibName:@“SecondView”bundle:nil]; sv.modalTransitionStyle = UIModalTransitionStyleFlipHorizo​​ntal; [self presentModalViewController:sv animated:YES]; – Gyani 2010-11-10 04:08:27

+0

感谢一群gyani和托马斯以及。由于涉及的代码较少,我很喜欢gyani的方法。再次感谢 :) – Veeru 2010-11-10 07:16:21

0

如果您已经厦门国际银行,你想给导航到另一个控制器,那么你必须在申报导航appdelegate.m 写下面的代码到App

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

然后在ViewController.m

- (IBAction)NextButtonClicked:(id)sender 
{ 
    yourNextViewController *objyourNextViewController = [[yourNextViewController alloc] initWithNibName:@"yourNextViewController" bundle:nil]; 
    [self.navigationController pushViewController:objStartUpViewController animated:TRUE]; 
} 
相关问题