2009-12-20 38 views
0

我发现很多关于在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 

回答

0

那么,如果您使用presentmodalviewcontroller并将animated设置为NO,那么他们将不会被置于他们回头的印象中,否则就没有其他方法可以知道我的

1

你可以尝试stmh这样

[myNavigationController setViewControllers:[NSArray arrayWithObject:viewControllerB] animated:NO]; 
0

你的应用程序的主视图中应包含一个子视图是最初查看A.主视图没有有任何东西在里面。

要切换视图,只需删除A并将其替换为新视图。你甚至可以对新视图进行动画处理,以便从任何位置滑入,或者放大或缩小或任何其他视图。

这是非常微不足道的,我一直这样做。我的主视图有一个标题栏(它只是一个UILabel)和一个工具栏(UIToolbar)。两者之间是我的“文件”,可以换成另一个文件。

+0

谢谢。我尝试了类似的东西。在View A的ViewController中,我有[self.view removeFromSuperview];但是这似乎并没有真正消除这种观点? – 2009-12-20 20:09:56

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]; 
} 
+0

我已经试过这个,但removeFromSuperview似乎没有工作,新的视图被添加,但旧的仍然存在 – 2009-12-20 23:46:19

+1

你可以提供一些示例代码显示你做了什么? removeFromSuperview绝对有效,如果你做了正确的事情,但没有办法让我们知道你是否没有发布代码。 – 2009-12-21 08:03:16

+0

谢谢,我在原始问题中添加了一些代码。 – 2009-12-21 14:21:11

相关问题