现在的位置: 首页 > 综合 > 正文

UIViewController之间的通讯方法

2016年11月18日 ⁄ 综合 ⁄ 共 863字 ⁄ 字号 评论关闭

两个View之间传递数据的方法有至少三种 :

1、用协议的方法
假如父窗口的中的响应函数是TestFn(NSString* text);

那么在子窗口中
先定义协议  
@protocol ChildViewDelegate<NSObject>
{
@optional
-(void) TestFn:(NSString*) text;

}
@end
然后在ChildView中定义这个变量
@property (nonatomicassignid <ChildViewDelegate>
delegate;
然后响应对应的函数
if([self.delegate respondsToSelector:@selector(TestFn:)])
{
[self.delegate TestFn:@“hahah”];
}
2、使用类似于C++的传入窗口指针,然后直接调用指针方法 
直接定义变量  UIViewControl* pPC ; 
然后调用[pPC TestFn:@“hahha”];
3、用Notication
其中的Name 最好是创建一个宏。
父窗口的响应函数是 TestFn
现实创建notication  

 [ [NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TestFn:) name:@"kTestFnNocation" object:nil];

然后子窗口post

   

    [[NSNotificationCenter defaultCenter] postNotificationName:kChangeLabelTextNotification object:_textField.text];

  

然后接收

-(void) TestFn: (NSNotification *)notification

{

    ModalViewController* CurViewC  = notification.object  ;

    NSLog( CurViewC->_textField.text); 

}

抱歉!评论已关闭.