新建了一个single view application的工程,添加了4个其它的view,想做到在这几个view窗口中自由切换,不知道能不能实现,还是新手,请教大家了
每个窗口中5个按钮,这样方便切换到其它的视图#import <UIKit/UIKit.h>
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#import "FourViewController.h"
@interface ViewController : UIViewController
- (IBAction)gotoFirstView:(id)sender;
- (IBAction)gotoSceondView:(id)sender;
- (IBAction)gotoThirdView:(id)sender;
- (IBAction)gotoFourView:(id)sender;
@property (retain, nonatomic)  FirstViewController *firstViewController;
@property (retain, nonatomic)  SecondViewController *secondViewController;
@property (retain, nonatomic)  ThirdViewController *thirdViewController;
@property (retain, nonatomic)  FourViewController *fourViewController;
@end上面是主View中的代码
点击每个BUTTON以后的代码如下
- (IBAction)gotoFirstView:(id)sender {
    if (self.firstViewController == nil) {
        firstViewController = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];
        [self.view addSubview:firstViewController.view];
       
       
    }else {
        [self.view addSubview:firstViewController.view];//重新显示第一个窗口
    }
}- (IBAction)gotoSceondView:(id)sender {
    if (self.secondViewController == nil) {
        secondViewController = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
        [self.view addSubview:secondViewController.view];
    }
}- (IBAction)gotoThirdView:(id)sender {
    if (self.thirdViewController == nil) {
        thirdViewController = [[ThirdViewController alloc]initWithNibName:@"ThirdViewController" bundle:nil];
        [self.view addSubview:thirdViewController.view];
    }
}- (IBAction)gotoFourView:(id)sender {
    if (self.fourViewController == nil) {
        fourViewController = [[FourViewController alloc]initWithNibName:@"FourViewController" bundle:nil];
        [self.view addSubview:fourViewController.view];
    }
}
用这样的方式我能切换到其它的窗口在FirstViewController中要切回主窗口我用以下代码
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSLog(@"FirstViewDisplay");
}
- (void) viewWillAppear:(BOOL)animated{
    NSLog(@"FirstViewDisplay viewWillAppear");
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    NSLog(@"FirstViewRemove");
}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}- (IBAction)gotoMainView:(id)sender {//将第一个窗口移除
    
    [self.view removeFromSuperview];
}- (IBAction)gotoSecondView:(id)sender {
}- (IBAction)gotoThirdView:(id)sender {
    
}- (IBAction)gotoFourView:(id)sender {
    
}
这样我可以实现跟主FirstView跟主View的切换
 第一次切换到FirstViewController的viewDidLoad函数会调用一下
有没有办法每次切换到FirstViewController窗口的时候有消息通知呢
还有就是切换回主View的时候也有消息通知,因为主View的viewDiidLoad也只会调用一次,以后再切换就不会调用想实现这5个按钮VIEW间的相互切换,以及有消息通知,请教各位前辈了

解决方案 »

  1.   

    NSNotification这个可以发送消息,你可以看看。肯定可以解决你的问题。但是视图切换不是你这么搞的,可能你还不了解iphone的view,和windows的窗体是有本质区别的。慢慢学吧。
      

  2.   

    谢谢你的回答,我也刚开始搞这个,思维还是停留在windows方面,但是业务有需求都嘛,需要几个窗体见能相互切换