基于IPhone视图的应用程序转到主视图
在基于视图的应用程序中,它包括三个视图。第一个视图(主视图)调用第二个视图。第二个视图调用第三个视图(详细视图)。我想在第三个视图中添加home函数以返回第一个视图(主视图)。 目前我在第三个视图中添加了Home按钮,但我不知道如何编写这个函数。以下是我的代码:基于IPhone视图的应用程序转到主视图,iphone,ios,cocoa-touch,Iphone,Ios,Cocoa Touch,在基于视图的应用程序中,它包括三个视图。第一个视图(主视图)调用第二个视图。第二个视图调用第三个视图(详细视图)。我想在第三个视图中添加home函数以返回第一个视图(主视图)。 目前我在第三个视图中添加了Home按钮,但我不知道如何编写这个函数。以下是我的代码: //In First view(main view) //call second view(list view) in button click -(IBAction) doctorList:(id)sender { if
//In First view(main view)
//call second view(list view) in button click
-(IBAction) doctorList:(id)sender
{
if(self.doctorViewController==nil)
{
DoctorViewController *doctorView=[[DoctorViewController alloc] initWithNibName:@"DoctorViewController" bundle:[NSBundle mainBundle]];
self.doctorViewController=doctorView;
[doctorView release];
}
self.view addSubview:self.doctorViewController.view];}
}
//In second View(list view)
//Call third view in table cell click
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
DoctorItem *physician=[[DoctorItem alloc]init];
physician=(DoctorItem*)[self.tableDataList objectAtIndex:indexPath.row];
DoctorDetail *detail=[[DoctorDetail alloc]init];[tableView.superview addSubview:detail.view];
[physician release];
}
在第三个视图中,有一个带有标签文本“Home”的按钮
当用户单击此按钮时,我想添加返回第一个视图(主视图)。我不知道怎么做 由于您只是在视图控制器之间导航,您可以在最后一个视图控制器中执行
popToRootViewController
,这将带您回到rootViewController
(您的主视图) 由于您只是在视图控制器之间导航,您可以在最后一个视图控制器中执行popToRootViewController
,这将带您回到rootViewController
(您的主视图) 我在凤凰城的帖子中没有看到任何提到UINAVIGATIONController的内容。。。因此,如果我正在修复此代码,我会创建一个iAction方法,如下所示:
- (IBAction) homeButtonPressed: (id) sender
{
// and remove the subviews
NSArray * subviews = [self.view subviews];
for(UIView * aSubview in subviews)
{
// this removes and releases the subviews you allocated
[aSubview removeFromSuperview];
}
}
并将您的按钮操作连接到该方法
另外,我建议为DoctorView和DoctorViewController子类化UIView而不是UIViewController。如果只是将子视图添加到根视图控制器上,您不需要UIViewController带来的额外负担。我在Phoenix的帖子中没有看到提到UINavigationController。。。因此,如果我正在修复此代码,我会创建一个iAction方法,如下所示:
- (IBAction) homeButtonPressed: (id) sender
{
// and remove the subviews
NSArray * subviews = [self.view subviews];
for(UIView * aSubview in subviews)
{
// this removes and releases the subviews you allocated
[aSubview removeFromSuperview];
}
}
并将您的按钮操作连接到该方法
另外,我建议为DoctorView和DoctorViewController子类化UIView而不是UIViewController。如果只是将子视图添加到根视图控制器上,则不需要UIViewController带来的额外负担。Phoenix Kyaw
试试这个
[self.navigationController pushViewController:homeView animated:YES];
这里homeView是您的homeViewController(无论您想去哪里)实例。Phoenix Kyaw
试试这个
[self.navigationController pushViewController:homeView animated:YES];
此处homeView是您的homeViewController(无论您想去哪里)实例。请尝试使用此实例
[self.view.superview removeFromSuperview];
请试着用这个
[self.view.superview removeFromSuperview];
试试这个
[self.navigationController pushViewController:homeView animated:YES];
这对我有用
[self.view addSubview:firstViewController.view];
或者试试这个
[self presentModalViewController:firstViewController animated:YES]; // this is deprecated in ios6.0
试试这个
[self.navigationController pushViewController:homeView animated:YES];
这对我有用
[self.view addSubview:firstViewController.view];
或者试试这个
[self presentModalViewController:firstViewController animated:YES]; // this is deprecated in ios6.0
我测试了您的代码。它无法解决我的问题。它仅从第三个视图中删除所有ui子视图。我的意思是,当用户单击“主页”按钮时,我想返回主视图(第一个视图)。如果将该方法放入主根控制器,它将从根(第一个)视图控制器中删除所有子视图(第二个和第三个)。但是,是的,一般来说,使用UINavigationController和推/弹出东西更可取。:-)我将该方法放在我的主根控制器中。我不知道如何从第三个视图调用此方法。我通过AppDelegate类调用了此方法。但它清除了所有子视图,包括TextView、label。实际上,我只想删除第二个和第三个视图。我如何知道此子视图是第二个视图还是第三个视图?我找到了解决方案。要检查视图类型,可以使用isKindOfClass方法。'code'如果([some_object isKindOfClass:[A_Class_Name Class]]){}我测试你的代码。它不能解决我的问题。它只从第三个视图中删除所有ui子视图。我的意思是,当用户单击home按钮时,我想返回主视图(第一个视图)。如果你将该方法放在主根控制器中,它将删除所有子视图(第二个和第三个)从根(第一个)视图控制器。但是,是的,一般来说,使用UINavigationController和push/popping内容更可取。:-)我将该方法放在我的主根控制器中。我不知道如何从第三个视图调用此方法。我通过AppDelegate类调用了此方法。但它清除了所有子视图,包括TextView、label。实际上,我只想删除第二个和第三个视图。我如何知道此子视图是第二个视图还是第三个视图?我找到了解决方案。要检查视图类型,如果([some_object iskindof Class:[A_Class_Name Class]]){,则可以使用iskindof Class方法。'code'