NSNotificationCenter全局推送

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

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

效果图:

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

#import <UIKit/UIKit.h>

@class ViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    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/p/2797356.html