ios delegate代理,多个/不同/两个view之间传递数据,传递批量数据,多个数据

要在view之间传数据大家都知道用delegate,我这里谈一下心得,适合新手。

网上一搜一大把的传递数据,但是都是单个数据,比如-(void)passValue:(NSString*)str;

这有个毛用,我们手机界面那个大,传一个数据有个锤子用啊?听说罗永浩在富士康附近宾馆住了半个月,问题还解决不了,你说你一个搞英语的,非要去搞手机,好歹雷军和比尔盖茨也是程序员,这下玩大了。

回过来讲,要传递批量数据咋办呐?一开始我想用C++思路,传结构体,多简单呐!结果ARC不允许在结构体放NSString之类的(初学的,勿笑)。问了cocoachina后原来可以自己定义类,呵呵。

原型参考这篇文章,说的正好是批量传递的:

http://blog.csdn.net/ryantang03/article/details/7915045

不过里面的代码比较老了,我们现在用的是纯粹storyboard,没有了如下代码:

//点击进入第二个窗口的方法
- (IBAction)openBtnClicked:(id)sender {
    SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]];
    //设置第二个窗口中的delegate为第一个窗口的self
    secondView.delegate = self;
    
    [self.navigationController pushViewController:secondView animated:YES];
    [secondView release];
}

取而代之的是prepareSegue,注意设置push的storyboard id

//在切换时此方法被调用,必须指定模态窗体的delegate为谁,否则模态窗体的delegate为nil
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.identifier isEqualToString:@"segIDLeftWin"]){
        LeftBtnViewController *srcView =segue.destinationViewController;
        srcView.delegate = self; //指定模态窗口的delegate
    }
}


在切换到下一个view的时候调用prepareSegue,这样就可以提前指定delegate是谁,如果delegate按照上面那个网址定义的话,那编译器会报警告的,原因是类型不匹配。

总结一下:

1、新建一个h和m文件,专门定义数据结构类,注意类型要指定@property,m文件要指定@synthesize,这样才能像C语言一样用“点”来引用,OC是不是很强大?

2、可以把协议/代理也放到上面的h文件声明,这样对一个工程来说比较集中,各有所好

自定义delegate方法可以参考这里:http://blog.csdn.net/chengyingzhilian/article/details/7873365

3、修饰delegate的@property最好用id类型,否则容易引起编译器的类型不匹配警告:

@property (nonatomic,weak)id delegate;

4、在实现代理函数的类加入:

#pragma mark - dataPassGroupDelegate

同时要在h文件的类定义里加入<>表示使用这个delegate

附代码:传递数据类的h文件:

//定义一个多个数据的类,用来传递数据
@interface dataPassGroup : NSObject{
    NSString *userName;
    NSString *gender;
    int age;
}
@property(nonatomic,strong) NSString *userName;
@property(nonatomic,strong) NSString *gender;
@property(assign)int age;

@end

传递数据类的m文件

#import "DataPassGroup.h"

@implementation dataPassGroup

//#synthesize关键字: 根据@property设置,自动生成成员变量相应的存取方法,从而可以使用 点操作符 来方便的存取该成员变量
@synthesize userName,gender,age;

@end

被调用view的delegate定义(h文件):

//实现一个delegate
@protocol dataPassGroupDelegate

-(void)LeftWindowDidCancle;
-(void)LeftWindowDidDone:(dataPassGroup *)value;

@end

//本窗口的类实现
@interface LeftBtnViewController : UIViewController{
    
    __weak IBOutlet UITextField *edtDataInput;
    __weak IBOutlet UITextField *edtNameInput;
    __weak IBOutlet UITextField *edtAgeInput;
    
    dataPassGroup *values;

}
- (IBAction)onBtnLeftWinBackPress:(id)sender;
- (IBAction)onBtnLeftWinDonePress:(id)sender;

//还需要声明这个delegate
@property (nonatomic,weak) id delegate;
@end




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在C#中,可以使用多线程和委托来实现多个串口数据传输到UI的功能。具体步骤如下: 1. 创建多个串口对象,分别打开并设置参数。 ``` SerialPort port1 = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One); SerialPort port2 = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One); port1.Open(); port2.Open(); ``` 2. 创建多个线程,分别使用不同的串口对象进行数据接收。 ``` Thread thread1 = new Thread(() => { while (true) { if (port1.BytesToRead > 0) { byte[] buffer = new byte[port1.BytesToRead]; port1.Read(buffer, 0, buffer.Length); // 将数据通过委托传递到UI线程 this.Invoke(new Action(() => { textBox1.AppendText(Encoding.ASCII.GetString(buffer)); })); } } }); Thread thread2 = new Thread(() => { while (true) { if (port2.BytesToRead > 0) { byte[] buffer = new byte[port2.BytesToRead]; port2.Read(buffer, 0, buffer.Length); // 将数据通过委托传递到UI线程 this.Invoke(new Action(() => { textBox2.AppendText(Encoding.ASCII.GetString(buffer)); })); } } }); thread1.Start(); thread2.Start(); ``` 3. 在UI线程中创建委托,用于将数据显示到UI控件上。 ``` private delegate void UpdateTextDelegate(string text); private void UpdateTextBox1(string text) { textBox1.Text = text; } private void UpdateTextBox2(string text) { textBox2.Text = text; } ``` 4. 在线程中调用委托,将数据传递到UI线程。 ``` this.Invoke(new UpdateTextDelegate(UpdateTextBox1), new object[] { text }); ``` 通过以上步骤,就可以实现多个串口数据传输到UI的功能。需要注意的是,在使用多线程时,要避免UI线程被阻塞,以免出现界面卡顿等问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值