星期四, 12月 29, 2011

ios iphone/ipad Dev 學習筆記 (A-03) 兩個 view 互相切換 - switch views

環境:Xcode 4.2 + iOS SDK 5
目標:
  • 延續前例 
  • 再增加一個 View(SecondViewController),透過 homeViewController 中的 button 觸發動作,將 current view 由 homeViewController 切換到 SecondViewController
說明:
  • 參考 學習筆記 (2)及(3)建立另一個 ViewController,命名為 SecondViewController 
  • 編輯 SecondViewController.h
  #import <UIKit/UIKit.h>

  @interface SecondViewController : UIViewController

  @property (nonatomic,strong)UILabel * labelOn2ndView;
  @property (nonatomic,strong)UIButton * btnOn2ndView;

  @end


  • 編輯 SecondViewController.m
  #import "HomeViewController.h"

  @implementation SecondViewController
  @synthesize labelOn2ndView = _labelOn2ndView;
  @synthesize btnOn2ndView = _btnOn2ndView;

  HomeViewController *homeViewController;

  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  {
      self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
      if (self) {
          // Custom initialization
      }
      return self;
  }

  - (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.
  }

  #pragma mark - View lifecycle

  - (void)viewDidLoad
  {
          //=========================================================================
          //建立一個 Label View
          CGRect frame = CGRectMake(20, 10, 280, 50);
          self.labelOn2ndView = [[UILabel alloc] initWithFrame:frame];
          self.labelOn2ndView.textAlignment = UITextAlignmentCenter;
          self.labelOn2ndView.font = [UIFont fontWithName:@"Verdana" size:20];
          self.labelOn2ndView.text = @"2nd View Controller";
    
          //建立一個 Button View
          frame = CGRectMake(20, 160, 280, 50);
          self.btnOn2ndView = [UIButton buttonWithType:UIButtonTypeRoundedRect];
          self.btnOn2ndView.frame = frame;
          [self.btnOn2ndView setTitle:@"OK" forState:UIControlStateNormal];
          self.btnOn2ndView.backgroundColor = [UIColor clearColor];
    
          //讓按鈕有生命,當按鈕被按到就呼叫 btn2Pressed 這個動作
          [self.btnOn2ndView addTarget:self action:@selector(btn2Pressed:) forControlEvents:UIControlEventTouchUpInside];
    
                   
          [self.view addSubview:self.labelOn2ndView];
          [self.view addSubview:self.btnOn2ndView];    
    
          //=========================================================================

          [super viewDidLoad];
  }


  - (void)viewDidUnload
  {
      [super viewDidUnload];
      // Release any retained subviews of the main view.
      // e.g. self.myOutlet = nil;
  }

  - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  {
      // Return YES for supported orientations
      return (interfaceOrientation == UIInterfaceOrientationPortrait);
  }

  -(void)btn2Pressed:(id)sender{
      NSLog(@"2ndView btn Pressed");
    
      // view 切換到 homeViewController
      // 將目標 view 產生出來
      homeViewController = [[HomeViewController alloc] initWithNibName:nil bundle:nil];
    
    
      // 把目標 view 掛進 自己的上一層 view,也就是 superview
      [self.view.superview addSubview:homeViewController.view]; 
        
      //將自己從 superview 中移除
      [self.view removeFromSuperview];
    
  }

  @end
  • 編輯 HomeViewController.m,將 -(void) btnClicked:(id)sender 這個 method 改成這樣
  -(void) btnClicked:(id) sender{
      NSLog(@"button clicked !!");
    
      secondViewController = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
    
      [self.view.superview addSubview:secondViewController.view];    
      [self.view removeFromSuperview];
}

    • 完工!!

      沒有留言: