消息传递之:IOS NSNotificationCenter,Android EventBus;
来源:互联网 发布:空白网名软件 编辑:程序博客网 时间:2024/05/16 09:19
文章来自:http://blog.csdn.net/intbird
IOS
IOS系统自带 NSNotificationCenter
0,上图
1,初始化程序入口
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { MainViewController* mainView = [[MainViewController alloc] init]; mainView.title = @"Delegate"; self.navigatarController = [[UINavigationController alloc]init]; [self.navigatarController pushViewController:mainView animated:YES]; self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]]; self.window.rootViewController = self.navigatarController; [self.window makeKeyAndVisible]; return YES; return YES;}
2,注册监听和处理方法
#import "MainViewController.h"#import "EventViewController.h"@interface MainViewController ()@end@implementation MainViewController- (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onEvent:) name:@"EVENT_KEY" object:nil];}-(void)onEvent:(NSNotification *)notifi{ NSString * message = notifi.name ; message = [message stringByAppendingString:notifi.object]; _labText.text = message; UIAlertController * alert = [UIAlertController alertControllerWithTitle:notifi.name message:notifi.object preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction * alertCacel = [UIAlertAction actionWithTitle:@"Cacel" style:UIAlertActionStyleCancel handler:nil]; [alert addAction:alertCacel]; [ self presentViewController:alert animated:YES completion:nil];}- (IBAction)sendMessage:(id)sender { EventViewController * eventView = [[EventViewController alloc] init]; [self.navigationController pushViewController:eventView animated:YES];}@end
3,调用事件方法
#import "EventViewController.h"@interface EventViewController ()@end@implementation EventViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib.}- (IBAction)sendMessage:(id)sender { [[NSNotificationCenter defaultCenter] postNotificationName:@"EVENT_KEY" object:@"post a message!"];}@end
Andorid使第三方EventBus
先看下系统自带的BroadcastReceiver
Anrdoid四大组件之一的BroadcastReceiver
动态注册广播接收器,处理 onReceiver(),
通过sendBroadcast(intent)方法发送广播;
private BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals(action)){ } } }; IntentFilter filter = new IntentFilter(); filter.addAction(action); registerReceiver(receiver, intentFilter);
再看下EventBus
官方demo说明:
https://github.com/greenrobot/EventBus/blob/master/HOWTO.md
0,上图
1,gradle dependencies
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.2.1' compile 'de.greenrobot:eventbus:3.0.0-beta1'}
2,注册监听,必须注意 @Subscribe public void method(){}
public class MainActivity extends AppCompatActivity { TextView tvText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EventBus.getDefault().register(this); findViewById(R.id.button_send).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this, EventBusActivity.class)); } }); tvText = (TextView)findViewById(R.id.textView); } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); } @Subscribe public void onEvent(EventBusEvent event) { String msg = event.getMessage(); tvText.setText(msg); Toast.makeText(this, msg, Toast.LENGTH_LONG).show(); }}
3,发送事件
public class EventBusActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_event); findViewById(R.id.button_send).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { EventBus.getDefault().post(new EventBusEvent("post a message!")); } }); }}
文章来自:http://blog.csdn.net/intbird
以上demo
IOS:https://github.com/intbird/IOSNSNotificationCenter
android:https://github.com/intbird/ANDIntbirdEventBus
0 0
- 消息传递之:IOS NSNotificationCenter,Android EventBus;
- Android之利用EventBus进行消息传递
- Android消息传递之EventBus 3.0使用
- Android EventBus 传递消息
- Android EventBus 传递消息
- Android消息传递利器EventBus
- Android消息传递之EventBus 3.0使用详解
- Android消息传递之EventBus 3.0使用详解
- Android消息传递之EventBus 3.0使用与案例
- (转)Android消息传递之EventBus 3.0使用详解
- Android消息传递之EventBus 3.0使用详解
- Android消息传递之EventBus 3.0使用详解(转载)
- Android消息传递之EventBus 3.0使用详解
- Android消息传递之EventBus 3.0使用详解
- Android消息传递之基于RxJava实现一个EventBus
- NSNotificationCenter 传递自定义消息
- IOS开发数据传递机制之NSNotificationCenter
- iOS设计模式之NSNotificationCenter 消息中心
- Android中你也许不知道的线性布局Layout_weight属性权重比例分配原理
- Python学习----Python基础
- Android虚拟机无法正常工作几例及解决办法
- iOS 多线程初学 - 多种方法创建多线程
- Java之旅EJB(1)——兵未动,粮草先行(jboss)
- 消息传递之:IOS NSNotificationCenter,Android EventBus;
- 手机上实现图片旋转
- Monkey的常见问题和解决办法
- Bitmap.createBitmap函数有6个重载方法
- 【C语言连载五】--------指针
- git 一直无法push的解决办法
- [学习笔记]A星寻路算法实例
- STL源码剖析-绪论
- 数据库面对不同业务逻辑约束条件的选择