2013年1月24日 星期四

動態產生 UIViewController (四)

連續使用兩層的動態UIViewController


1. 開啓新的專案

2.新增兩個UIViewController的Class檔

3. 首先在mainViewController.h加入變數
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
#import "SecondViewController.h"


@interface mainViewController : UIViewController

@property (nonatomic) FirstViewController *firstView;

@end



4. 在mainViewController.m加入到下一級的程式碼
#import "mainViewController.h"

@interface mainViewController ()

@end

@implementation mainViewController

@synthesize firstView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
   
    [self setGoFirstViewButton];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setGoFirstViewButton  // 動態產生一個的Button
{
    UIButton *startSayStoryButton = [UIButton  buttonWithType:UIButtonTypeRoundedRect];
    //動態產生一個RoundedRect 形式的  Button
   
    startSayStoryButton.frame = CGRectMake(0,0, 100, 100); // 大小
   
    //_helloActionButton.center = self.view.center;
   
    [startSayStoryButton setCenter:CGPointMake(300, 300)];//位置放在x=150, y=50的位置
   
   
    [startSayStoryButton addTarget:self action:@selector(activeView) forControlEvents:UIControlEventTouchUpInside];
    //設定Button動作呼叫的function在 onHelloActionButton,方式為按下
   
    //_helloActionButton.= @"Action Button";
    [startSayStoryButton setTitle:@"To FirstView" forState:UIControlStateNormal];
    //將動態Button上放置Action Button這兩個字
   
    [self.view addSubview:startSayStoryButton];
    //將動態Button放到View上展出
   
}

- (void) activeView
{
    firstView = [[FirstViewController alloc] init];
   
    firstView.view.backgroundColor = [UIColor blueColor];
   
    [firstView setValue:@"FirstView" forKey:@"transferString"];
   
    [self presentViewController:firstView animated:YES completion:^{}];
   
}

@end


5. 在FirstViewController.h加入變數
#import <UIKit/UIKit.h>
#import "SecondViewController.h"

@interface FirstViewController : UIViewController
{
    NSString *transferString;
}

@property (nonatomic) SecondViewController *secondView;


@end


6. 在FirstViewController.m加入程式碼
#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
    [self setGoBackButton];
    [self setGoSecondViewButton];

   
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) setGoBackButton
{
    //NSLog(@"%@", transferString);
   
    //self.view.backgroundColor = [UIColor yellowColor];
   
    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [aButton setFrame:CGRectMake(10, 10, 200, 50)];
    aButton.backgroundColor = [UIColor  redColor];
    //[aButton setTitle:@"Go Back" forState:UIControlStateNormal];
   
    [aButton setTitle:@"回去前一級" forState:UIControlStateNormal];
   
   
    [aButton addTarget:self action:@selector(goBackMainViewController) forControlEvents:UIControlEventTouchUpInside];
   
    [self.view addSubview:aButton];
   
}

- (void) goBackMainViewController
{
   
    [self dismissViewControllerAnimated:YES completion:^{}];
}

- (void)setGoSecondViewButton  // 動態產生一個的Button
{
    UIButton *startSayStoryButton = [UIButton  buttonWithType:UIButtonTypeRoundedRect];
    //動態產生一個RoundedRect 形式的  Button
   
    startSayStoryButton.frame = CGRectMake(0,0, 200, 100); // 大小
   
    //_helloActionButton.center = self.view.center;
   
    [startSayStoryButton setCenter:CGPointMake(300, 300)];//位置放在x=150, y=50的位置
   
   
    [startSayStoryButton addTarget:self action:@selector(activeView) forControlEvents:UIControlEventTouchUpInside];
    //設定Button動作呼叫的function在 onHelloActionButton,方式為按下
   
    //_helloActionButton.= @"Action Button";
    [startSayStoryButton setTitle:@"To SecondView" forState:UIControlStateNormal];
    //將動態Button上放置Action Button這兩個字
   
    [self.view addSubview:startSayStoryButton];
    //將動態Button放到View上展出
   
}

- (void) activeView
{
    secondView = [[SecondViewController alloc] init];
   
    secondView.view.backgroundColor = [UIColor greenColor];
   
    [secondView setValue:@"SecondView" forKey:@"transferString"];
   
    [self presentViewController:secondView animated:YES completion:^{}];
   
}


@end

7. 在SecondViewController.h加入變數
#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController
{
    NSString *transferString;
}

@end

8.在SecondViewController.m加入程式碼
#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
    [self setGoBackButton];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) setGoBackButton
{
    //NSLog(@"%@", transferString);
   
    //self.view.backgroundColor = [UIColor yellowColor];
   
    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [aButton setFrame:CGRectMake(10, 10, 200, 50)];
    aButton.backgroundColor = [UIColor  redColor];
    //[aButton setTitle:@"Go Back" forState:UIControlStateNormal];
   
    [aButton setTitle:@"回去前一級" forState:UIControlStateNormal];
   
   
    [aButton addTarget:self action:@selector(goBackMainViewController) forControlEvents:UIControlEventTouchUpInside];
   
    [self.view addSubview:aButton];
   
}

- (void) goBackMainViewController
{
   
    [self dismissViewControllerAnimated:YES completion:^{}];
}


@end


9. 結果圖




沒有留言: