THIS CONTENT DOWNLOAD SHORTLY

Objective

Main objective of this blog post is to give you an idea about Notification.

 

When you want to broadcast a message or information of object to other object in other viewcontroller of the application then NSNotificationCenter is used. You have to register object with notification center to receive notifications. When any object post notification then the object which is registered as observer in notification center will receive notification and call its selector method.

Lets create a demo application which use NSNotificationCenter.

 

Step 1 Create XCode Project

Open Xcode and create new project with single view application template as shown in below figure. 

create-single-view-application

In the next step enter NotificationDemo as a product name and also select Objective-C as a language as shown in below figure. 

notification-demo

 

Step 2 Design UI

Add a button set its title “Next”. Add other view controller and create segue from Next button to that viewcontroller. Add a button in that view controller give its title Post Notification as shown in below figure. 

setup-ui

Add new CocoaTouchClass (that is .h and .m file for viewcontroller.) from File -> file… -> select CocoaTouchClass name it NextViewController.

Now open storyboard select newly added viewcontroller and change its class to NextViewController from identity inspector. 

identity-inspector

 

Step 3 Add Observer for Notification

Add observer for notification in first view controller’s viewDidLoad method as shown in below code.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(testNotificationPosted:) name:@"TEST_NOTIFICATION" object:nil];

It will add observer for notification named “TEST_NOTIFICATION”. testNotificationPosted selector method called when notification named “TEST_NOTIFICATION” will be post.

Now add selector method that will display alert when notification posted. Selector has parameter named notification of type NSNotification. It contain object, which is passed at time when notification posted.

-(void)testNotificationPosted:(NSNotification *)notification
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notification!" message:[NSString stringWithFormat:@"Test notification fired with test object '%@'", notification.object] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
    alert = nil;
}
 
 

Step 4 POST Notification

Add action method for Post Notification button in NextViewController.m. Post notification named “TEST_NOTIFICATION” as shown in below code.

- (IBAction)btnPostNotificationClicked:(id)sender
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"TEST_NOTIFICATION" object:@"TestObject"];
}

In above code we have passed a string “TestObject” as notification object.

When user click on Post Notification button it will post notification named “TEST_NOTIFICATION” and will call selector method defined when observer added to the notification.

I hope you find this blog is very helpful while working with Notification in iOS. Let me know in comment if you have any questions regarding Notification Implementation in iOS. I will reply you ASAP.

Got an Idea of iPhone App Development? What are you still waiting for? Contact us now and see the Idea live soon. Our company has been named as one of the best iPhone App Development Company in India.

I am iOS developer with an aspiration of learning new technology and creating a bright future in Information Technology.

face mask Belial The Demon Headgear Pocket Staff Magic