我发现很多关于在iPhone上切换视图的教程,但没有一个与我的需求相符。我想知道你能否帮忙。如何以编程方式切换视图
我期待有一个初步的观点,A和按钮上按卸载,然后切换到查看B.
我见过的下面,但我有原因,我不想这样做:
使用导航控制器 - 一旦视图A被丢弃,我不需要返回它,所以它应该完全消失并从内存中释放。
使用选项卡栏控制器 - 与上述相同的原因。我想从A线性移动到B线而不是B线到A线。
使用模态视图 - 实际上,原因相同。此外,这感觉就像是一个用户界面不 - 不,因为它给用户的印象是,一旦B中的操作完成,他们就会回到视图A.
请帮助我,我真正挣扎的一件事是如何卸载作为应用程序的一部分创建的原始视图控制器。我想要显示其他视图,我需要做的就是init并将其分配到视图A中,然后将其添加为子视图。然而,从视图控制器A卸载视图A真的让我感到困惑。
在此先感谢!
编辑:从下面的人的建议我已经实现了视图切换,但出于某种原因,在显示视图B后,removeFromSuperview无法在视图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(你会看到,当相机选取器返回的图像,我试图通过调用应用程序的委托方法来切换视图:
#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];
}
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
NSLog(@"View loaded");
[super viewDidLoad];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (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
谢谢。我尝试了类似的东西。在View A的ViewController中,我有[self.view removeFromSuperview];但是这似乎并没有真正消除这种观点? – 2009-12-20 20:09:56