视图传值

1.简单方式

  使用UiViewController的两个属性获取源视图控制器和目标视图控制器:

    1.presentingViewController :

                  (ViewController *)self.presentingViewController.<属性名>;

    2.presentedViewController:

                  (ViewController *)self.presentedViewController.<属性名>;

2.委托

  委托使用@protocol的方式实现,又称为协议。

  首先定义一个委托MyDelegate传值

@protocol   myDeleget<NSObject>
-(void)setAViewValue:(NSString *)textValue;
@end

  在接受数据的视图类中添加协议

@interface AViewController : UIViewController<myDeleget>

@end

  在AViewController.m文件中实现协议中的方法

setAViewValue:(NSString *)textValue

{
 
   NSLog(@"%@",_textString);

    //A视图中被修改的属性
    _emailtext.text=textValue;

  
 }

  在传递数据的bViewController类中定义

  

@property(nonatomic,assign)  id<myDeleget> deleget;

  在接受数据类AViewController中将自身self委托

-(void)senderTouch:(id)sender
{
   
     _BView=[[bViewController alloc] init];
   
     _BView.deleget=self;
   
     [_BView.view setBackgroundColor:[UIColor whiteColor]];
  
      NSLog(@"%@",_BView.deleget);
   
     [self presentViewController:_BView animated:YES completion:^{
        
    }];
  
 }

  bViewController.m返回事件中实现委托

-(void)BackTouch:(id)sender
{
    if (_deleget != nil) {
        
        [_deleget setAViewValue:_bViewText.text];
        [self dismissViewControllerAnimated:YES completion:^{
            
            NSLog(@"%@",_bViewText.text);
        }];
    }
    
}

 

posted @ 2015-01-23 13:23  探出的头  阅读(150)  评论(0编辑  收藏  举报