在AppDelegate中添加UITabBarController
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
tabBarController = [[UITabBarController alloc] init];
ChinaViewController* vc1 = [[ChinaViewController alloc] init];
HubeiViewController* vc2 = [[HubeiViewController alloc] init];
NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2, nil];
tabBarController.viewControllers = controllers;
[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];
return YES;
}
ChinaViewController类中添加方法
-(id)init {
if ([super init] != nil) {
UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"China" image:nil tag:1];
self.tabBarItem = item;
[item release];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test:)
name:@"wangsong" object:nil];
}
- (void)test:(NSNotification*)notify{
NSString *testString = @"Test Notification!";
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Notification"
message:testString
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
[alertView release];
}
在HubeiViewController类中添加方法:
-(id)init {
if ([super init] != nil) {
UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"Hubei" image:nil tag:1];
self.tabBarItem = item;
[item release];
}
return self;
}
添加一个button的响应方法:
- (IBAction)btPressed{
[[NSNotificationCenter defaultCenter] postNotificationName:@"wangsong" object:nil];
}
点击这个button的时候,将会调用ChinaViewController中的(void)test:(NSNotification*)notify方法,弹出alertShow。