博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
NSNotificationCenter全局推送
阅读量:7049 次
发布时间:2019-06-28

本文共 3428 字,大约阅读时间需要 11 分钟。

到周末啦,可以睡懒觉了,不过该作的工作还是要做完。

今天为大家写了一篇关于NSNotificationCenter推送的,很简单,方便新手了解如何在整个程序发送推送通知。

效果图:

先在AppDelegate.h里面加两个全局变量

#import 
@class ViewController;@interface AppDelegate : UIResponder
{ NSInteger badgevalue; UITabBarItem *barOneItem;}@property (strong, nonatomic) UIWindow *window;@property (strong, nonatomic) ViewController *viewController;@end

再更改AppDelegate.m里面的代码

#import "AppDelegate.h"#import "ViewController.h"#import "BarOne.h"#import "BarTwo.h"@implementation AppDelegate@synthesize window = _window;@synthesize viewController = _viewController;- (void)dealloc{    [_window release];    [_viewController release];    [super dealloc];}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];    // Override point for customization after application launch.//    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];//    self.window.rootViewController = self.viewController;//把原来的这两句注销    UITabBarController *tabBar = [[UITabBarController alloc]init];    BarOne *barone = [[[BarOne alloc]init]autorelease];    UINavigationController *nav1 = [[[UINavigationController alloc]initWithRootViewController:barone]autorelease];    barOneItem = [[[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:0]autorelease];//注意这里将barOneItem定义成了实例变量,为了能够在别的方法中改变它的badgeValue值    nav1.tabBarItem = barOneItem;        BarTwo *bartwo = [[[BarTwo alloc]init]autorelease];    UINavigationController *nav2 = [[[UINavigationController alloc]initWithRootViewController:bartwo]autorelease];    UITabBarItem *barTwoItem = [[[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemHistory tag:1]autorelease];    nav2.tabBarItem = barTwoItem;        NSArray *barArray = [NSArray arrayWithObjects:nav1,nav2, nil];    tabBar.viewControllers = barArray;//这里说明一下,viewControllers属性是一个NSArray类型的,它存放的是一个一个UINavigationController,例如获得当前的viewController是[barArray count]-1    [self.window addSubview:tabBar.view];    [self.window makeKeyAndVisible];    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeValue) name:@"changevalue" object:nil];//注册观察者,准备接受任何类推送的通知,"changevalue" 这个名字要记住,在别的类里面发送消息推送也要用到这个名字。    return YES;}- (void)changeValue//改变badgeValue值{    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"通知+1" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];    [alert show];    [alert release];    barOneItem.badgeValue = [NSString stringWithFormat:@"%d",badgevalue];    badgevalue ++;}- (void)applicationWillResignActive:(UIApplication *)application{}- (void)applicationDidEnterBackground:(UIApplication *)application{}- (void)applicationWillEnterForeground:(UIApplication *)application{}- (void)applicationDidBecomeActive:(UIApplication *)application{}- (void)applicationWillTerminate:(UIApplication *)application{}@end

主要的工作就完成了,新建两个类BarOneBarTwo准备发送通知。

这两个类很简单,只在.m文件里增加了一个方法

-(IBAction)changvalue{    [[NSNotificationCenter defaultCenter]postNotificationName:@"changevalue" object:nil];}

再在xib文件里添加一个button和这个方法连起来。

BarTwo这个类里面添加一样的方法和button,就完成了,运行看看,点击每一个button都会调用AppDelegate里面的- (void)changeValue方法,实现了在不同的类里面全局推送消息。

周末偷懒中......

 

 

转载于:https://www.cnblogs.com/xiaobaizhu/archive/2012/12/01/2797356.html

你可能感兴趣的文章
【java设计模式】之 模板方法(Template Method)模式
查看>>
【踩坑速记】MIUI系统BUG,调用系统相机拍照可能会带给你的一系列坑,将拍照适配方案进行到底!...
查看>>
小米手机会不会更好
查看>>
atitit.Sealink2000国际海运信息管理系统
查看>>
android面试总结01 activity生命周期
查看>>
Java 实现策略(Strategy)模式
查看>>
Python文本爬虫实战
查看>>
leetcode:Gray Code
查看>>
IDEA+PHP+XDebug调试配置
查看>>
Jenkins
查看>>
Ubuntu离线安装Sogou拼音(附老版本安装&输入法自启动)
查看>>
springmvc结合base64存取图片到mysql
查看>>
深度学习主机环境配置: Ubuntu16.04+GeForce GTX 1080+TensorFlow
查看>>
linux 抓包 tcpdump 简单应用
查看>>
mongodb官网文档阅读笔记:与写性能相关的几个因素
查看>>
PHP处理时间格式
查看>>
BestCoder Round #11 (Div. 2)
查看>>
JAVA入门[20]-Spring Data JPA简单示例
查看>>
Python: The _imagingft C module is not installed错误的解决
查看>>
HTTP请求报文和HTTP响应报文
查看>>