天天看點

iOSm界面跳轉和參數傳遞之presentViewController與dismissViewControllerAnimated

假設

界面1為 ViewController01 : UIViewController 界面2為 ViewController02 : UIViewController 

其中界面1為項目的rootViewController.

AppDelegate.h代碼

C代碼  

iOSm界面跳轉和參數傳遞之presentViewController與dismissViewControllerAnimated

  1. #import <UIKit/UIKit.h>  
  2. @interface AppDelegate : UIResponder <UIApplicationDelegate>  
  3. @property (strong, nonatomic) UIWindow *window;  
  4. @property (strong, nonatomic) NSString* localImagePath;  
  5. @end  

AppDelegate.m的部分代碼

iOSm界面跳轉和參數傳遞之presentViewController與dismissViewControllerAnimated
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
  2.     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
  3.     // Override point for customization after application launch.  
  4.     ViewController01* mViewController01 = [[ViewController01 alloc]init];  
  5.     self.window.rootViewController = mViewController01;  
  6.     self.window.backgroundColor = [UIColor whiteColor];  
  7.     [self.window makeKeyAndVisible];  
  8.     return YES;  
  9. }  

ViewController01.h代碼

iOSm界面跳轉和參數傳遞之presentViewController與dismissViewControllerAnimated
  1. @interface ViewController01 : UIViewController  
  2. @property (strong,nonatomic) NSMutableDictionary* parameter;  

ViewController01.m代碼

iOSm界面跳轉和參數傳遞之presentViewController與dismissViewControllerAnimated
  1. #import "ViewController01.h"  
  2. #import "MyViewTool.h"  
  3. #import "ViewController02.h"  
  4. @interface ViewController01 ()  
  5. @implementation ViewController01  
  6. - (void)viewDidLoad {  
  7.     NSLog(@"viewDidLoad():視圖1");  
  8.     [super viewDidLoad];  
  9.     // Do any additional setup after loading the view.  
  10.     [self initViews];  
  11.     [self initParameter];  
  12. -(void)viewDidAppear:(BOOL)animated{  
  13.     NSLog(@"viewDidAppear():視圖1");  
  14.     [super viewDidAppear:animated];  
  15.     //接收到的參數  
  16.     NSLog(@"viewDidAppear():視圖1,收到的參數:from=%@",[self.parameter objectForKey:@"from"]);  
  17. - (void)initViews{  
  18.     CGSize size = [MyViewTool loadScreenSize];  
  19.     CGFloat width = size.width;  
  20.     UILabel* label = [MyViewTool createLabel:CGRectMake(10, 40, width-20, 100) withText:@"視圖1" withBgcolor:[UIColor yellowColor]];  
  21.     UIButton* btn = [MyViewTool createButton:CGRectMake(label.frame.origin.x, label.frame.origin.y+label.frame.size.height+20, 200, 50) withDelegate:self withAction:@selector(buttonClick:) withTitle:@"按鈕1:點選打開視圖2"  
  22.                                  withBgColor:[UIColor lightGrayColor]];  
  23.     btn.center = CGPointMake(width/2, btn.center.y);  
  24.     [self.view addSubview:label];  
  25.     [self.view addSubview:btn];  
  26. #pragma mark 初始化要傳遞的參數  
  27. - (void)initParameter{  
  28.     self.parameter = [[NSMutableDictionary alloc]init];  
  29. -(void)buttonClick : (UIButton*) sender {  
  30.     NSLog(@"buttonClick:%@",sender.titleLabel.text);  
  31.     //設定傳遞參數的資料  
  32.     [self.parameter setObject:@"我是視圖1設定的參數" forKey:@"from"];  
  33.     //打開 ViewController02  
  34.     ViewController02* mViewController02 = [[ViewController02 alloc]init];  
  35.     mViewController02.parameter=self.parameter;  
  36.     [self presentViewController:mViewController02 animated:YES completion:^{  
  37.         NSLog(@"presentViewController成功");  
  38.     }];  
  39. - (void)didReceiveMemoryWarning {  
  40.     [super didReceiveMemoryWarning];  
  41.     // Dispose of any resources that can be recreated.  

ViewController02.h代碼

iOSm界面跳轉和參數傳遞之presentViewController與dismissViewControllerAnimated
  1. @interface ViewController02 : UIViewController  

ViewController02.m代碼

iOSm界面跳轉和參數傳遞之presentViewController與dismissViewControllerAnimated
  1. @interface ViewController02 ()  
  2. @implementation ViewController02  
  3.     NSLog(@"viewDidLoad():視圖2");  
  4.     NSLog(@"viewDidAppear():視圖2");  
  5.     NSLog(@"viewDidAppear():視圖2,收到的參數:from=%@",[self.parameter objectForKey:@"from"]);  
  6.     UILabel* label = [MyViewTool createLabel:CGRectMake(10, 40, width-20, 100) withText:@"視圖2" withBgcolor:[UIColor yellowColor]];  
  7.     UIButton* btn = [MyViewTool createButton:CGRectMake(label.frame.origin.x, label.frame.origin.y+label.frame.size.height+20, 200, 50) withDelegate:self withAction:@selector(buttonClick:) withTitle:@"按鈕2:點選傳回視圖1" withBgColor:[UIColor lightGrayColor]];  
  8.     //設定傳回給視圖1的參數  
  9.     [self.parameter setObject:@"我是視圖2設定的參數" forKey:@"from"];  
  10.     [self dismissViewControllerAnimated:YES completion:^{  
  11.         NSLog(@"dismissViewControllerAnimated成功");  

 項目源代碼參見附近中的demo010.zip

繼續閱讀