在按钮上更改viewController单击
我是iPhone编程的新手。我正在尝试的是我有一个按钮的屏幕。我想改变视图控制器不仅仅是视图,当我点击该按钮(我知道如何添加子视图),因为从第二视图控制器,我必须去第三个视图,这是不可能的,如果我在第一次添加子视图地点。有人可以帮我吗?这可能吗?如果是,如何?所有视图和视图控制器都是通过编程创建的。我没有使用IB。在按钮上更改viewController单击
编辑:这里是相关的代码点击按钮
-(id)showCurrentLoc:(id)sender {
locationController = [currentLocController alloc];
[entry removeFromSuperview];
[newLoc removeFromSuperview];
[currentLoc removeFromSuperview];
[self.view setBackgroundColor:[UIColor clearColor]]; //[self.view addSubview: [locationController view]];
[self.navigationController pushViewController:locationController animated:YES]; [locationController release];
return 0;
} //Location Controller is the tableViewController
感谢 维克
通常情况下,您会使用导航控制器进行此类操作,以便用户可以轻松地返回到上一个视图。然后,您的视图控制器会做这样的事情:
[self.navigationController pushViewController:someNewViewController animated:YES];
如果你想自己管理视图控制器,你可以永远只是改变窗口的rootViewController
财产。请阅读View Controller Programming Guide了解完整图片。
@ Caleb-这是我amtrying,但事情是我想lad tableViewController作为我的第二个viewController,但这不工作。如果我只是添加[self.view addSubView:[xyztableview view]]。然后我可以看到我的表格视图。我不知道第一种方法有什么问题吗? – Vik 2011-04-28 07:00:30
@Vik对不起 - 我无法理解你的评论。既然你是OP,我建议你编辑你的问题来更清楚地解释实际问题。然而,在你这样做之前,请仔细看看我引用的文档。这是iOS开发人员必读的内容,它绝对解释了如何从一个控制器移到另一个控制器。 – Caleb 2011-04-28 07:08:01
- (id)showCurrentLoc:(id)发送者 { \t \t locationController = [currentLocController alloc]; \t [entry removeFromSuperview]; \t [newLoc removeFromSuperview]; \t [currentLoc removeFromSuperview]; \t [self.view setBackgroundColor:[UIColor clearColor]]; // [self.view addSubview:[locationController view]]; \t [self.navigationController pushViewController:locationController animated:YES]; \t [locationController release]; \t return 0; } //位置控制器是tableViewController – Vik 2011-04-28 07:12:15
UINavigationController就是你需要的时候触发。它管理一堆UIViewController
,如果你想添加新的UIViewController,只需将它推入此导航堆栈。它为你自动执行后退按钮行为,并且每当你完成它时,你就可以从栈中弹出当前的UIViewController。
@ Krishnabhadra-这是我amtrying,但事情是我想lad tableViewController作为我的第二个viewController,但这不工作。如果我只是添加[self.view addSubView:[xyztableview view]]。然后我可以看到我的表格视图。我不知道第一种方法有什么问题吗? – Vik 2011-04-28 07:00:14
应该work..Can您发布相关的代码,以便我们可以交叉检查 – Krishnabhadra 2011-04-28 07:01:58
这里是有关的代码,火灾时,点击按钮 - (ID)showCurrentLoc:(ID)发送 { \t \t locationController = [currentLocController页头] ; \t [entry removeFromSuperview]; \t [newLoc removeFromSuperview]; \t [currentLoc removeFromSuperview]; \t [self。查看setBackgroundColor:[UIColor clearColor]]; // [self.view addSubview:[locationController view]]; \t [self.navigationController pushViewController:locationController animated:YES]; \t [locationController release]; \t return 0; } //位置控制器是tableViewController – Vik 2011-04-28 07:13:10
你可以做这样的事情
YourViewController *objYourViewController = [[YourViewController alloc] initWithNibName:@"YourViewController" bundle:nil];
[self.navigationController pushViewController:objYourViewController animated:YES];
[YourViewController release];
这就是我amtrying,但事情是我想lad tableViewController作为我的第二个viewController,但这不工作。如果我只是添加[self.view addSubView:[xyztableview view]]。然后我可以看到我的表格视图。我不知道第一种方法有什么问题吗? – Vik 2011-04-28 06:59:49
@Vik你如何定义tableViewController即声明?你可以请张贴代码为你的tableViewController创建对象吗? – 2011-04-28 12:21:21
@ Jennie-谢谢我能够在代理中声明它后,使用导航控制器更改视图控制器。 – Vik 2011-04-28 12:50:05
你可以用一个UINavigationController工作。将您的第一个UIViewController中像这样的init方法:
[self setViewControllers:[NSArray arrayWithObject:viewController]];
然后当按钮被点击或作出选择,你推与第二少数控制器(在第一个的viewController):
[self.navigationController pushViewController:controller animated:YES];
这样你也会得到一个自动(后退按钮)。基本上,你创建了一堆UIViewControllers,你可以像普通栈一样推送和弹出。
我希望这会有所帮助。看看以下内容: UINavigationController Class Reference
- (void) loadViewAtIndex:(NSInteger)index {
[self unloadViewAtIndex:activeViewIndex];
switch (index) {
case 1:
{
if (viewController1 == nil)
{
viewController1 = [[ViewController1 alloc] initWithNibName:@"ViewController1" bundle:nil];
}
viewController1.view.frame = CGRectMake(0.0, 0.0, viewController1.view.frame.size.width, viewController1.view.frame.size.height);
[window addSubview:viewController1.view];
}
break;
case 2:
{
if (viewController2 == nil)
{
viewController2 = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];
}
viewController2.view.frame = CGRectMake(0.0, 0.0, viewController2.view.frame.size.width, viewController2.view.frame.size.height);
[window addSubview:viewController2.view];
}
break;
default:
break;
}
activeViewIndex = index;
}
- (void) unloadViewAtIndex:(NSInteger)index {
switch (index) {
case 1:
{
if (viewController1 != nil)
{
[viewController1.view removeFromSuperview];
[viewController1 release];
viewController1 = nil;
}
}
break;
case 2:
{
if (viewController2 != nil)
{
[viewController2.view removeFromSuperview];
[viewController2 release];
viewController2 = nil;
}
}
break;
default:
break;
}
}
您可以使用导航控制器。 – saadnib 2011-04-28 06:53:52