我发现很多关于在iPhone上切换视图的教程,但没有一个完全符合我的需求。我想知道你是否可以提供帮助。
我希望有一个初始视图,A和按钮按下卸载A并切换到视图B.
我见过以下内容,但我有理由不这样做:
使用导航控制器 - 一旦视图A被丢弃,我不需要返回它,因此它应该完全消失并从内存中释放。
使用标签栏控制器 - 与上述相同的原因。我想从A线性移动到B线而不是B线圈移动到A线。
使用模态视图 - 实际上,同样的原因。此外,这感觉就像一个用户界面禁忌,因为它给用户的印象是,一旦B中的动作完成,他们就会回来查看A.
请帮助我,我真正挣扎的一件事是如何卸载作为应用程序一部分创建的原始视图控制器。我想要显示另一个视图,我需要做的是init并在View A中分配它,然后将其添加为子视图。但是,从viewcontroller A卸载视图A实在令我感到困惑。
编辑:根据以下某人的建议,我实现了视图切换,但由于某些原因,在显示视图B后,removeFromSuperview在View A上不起作用。基本上我仍然可以在视图B后面看到视图A(我使视图b略小,所以我可以测试它。)
这是我的应用代表:
@implementation View_SwitchAppDelegate
@synthesize window;
@synthesize switchViewController;
@synthesize secondViewController;
@synthesize firstViewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after application launch
FirstViewController *aFirstView = [[FirstViewController alloc] initWithNibName:@"FirstView" bundle:nil];
[self setFirstViewController:aFirstView];
[aFirstView release];
[window addSubview:firstViewController.view];
[window makeKeyAndVisible];
}
- (void)dealloc {
[window release];
[switchViewController release];
[super dealloc];
}
- (void)switchToTwo {
SecondViewController *aSecondView = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:nil];
[self setSecondViewController:aSecondView];
[aSecondView release];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:window cache:YES];
[firstViewController.view removeFromSuperview];
[firstViewController release];
firstViewController = nil;
[self.window addSubview:[secondViewController view]];
[window makeKeyAndVisible];
[UIView commitAnimations];
}
@end
这是我的FirstViewController(您会看到当相机选择器返回图像时,我尝试通过调用app delegate方法来切换视图:
#import "FirstViewController.h"
#import "View_SwitchAppDelegate.h"
#import "SecondViewController.h"
@implementation FirstViewController
- (IBAction) takeButtonPressed
{
NSString *type = @"Camera";
[self callCameraWithType:type];
}
- (IBAction) chooseButtonPressed
{
NSString *type = @"Library";
[self callCameraWithType:type];
}
-(void) callCameraWithType:(NSString *)type {
// Set up the image picker controller and add it to the view
imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
if ([type isEqual:@"Camera"])
{
NSLog(@"Camera");
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
imagePickerController.sourceType =
UIImagePickerControllerSourceTypeCamera;
}
else {
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
}
else
{
NSLog(@"Library");
imagePickerController.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
}
[self presentModalViewController:imagePickerController animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
//get app delegate
View_SwitchAppDelegate *appDelegate = (View_SwitchAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate switchToTwo];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
// Dismiss the image selection and close the program
[picker dismissModalViewControllerAnimated:YES];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
NSLog(@"View loaded");
[super viewDidLoad];
}
- (void)removeSelf{
[self.view removeFromSuperview];
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
NSLog(@"First Unload");
}
- (void)dealloc {
NSLog(@"First Dealloc");
[super dealloc];
}
@end
答案 0 :(得分:1)
你可以试试这样的
[myNavigationController setViewControllers:[NSArray arrayWithObject:viewControllerB] animated:NO];
答案 1 :(得分:1)
为什么不滚动你自己的视图控制器,当你调用时删除一个视图并替换另一个?,像这样
if (firstViewController.view.superview !=nil)
{
if(self.secondViewController == nil)
{
secondViewController *secondViewController = [[secondViewController alloc] initWithNibName:@"secondSavesView" bundle:nil];
self.secondViewController = secondViewController;
[secondViewController release];
}
[firstViewController.view removeFromSuperview];
//probably include some animation here
[detailView insertSubview:secondViewController.view atIndex:0];
}
答案 2 :(得分:0)
好吧,如果你使用presentmodalviewcontroller
并将animated
设置为NO,那么他们就不会觉得他们会回来。否则,我不知道其他任何方式。
答案 3 :(得分:0)
您的应用程序的主视图应包含一个初始视图A的子视图。主视图中不必包含任何其他内容。
要切换视图,只需删除A并将其替换为新视图即可。您甚至可以为新视图设置动画,使其从任何地方滑入,或放大或淡入,或者其他任何内容。
这非常简单,我一直这样做。我的主视图有一个标题栏(只是一个UILabel)和一个工具栏(UIToolbar)。两者之间是我的“文件”,可以换成另一个。