NSNotificationCenter postNotificationName exec_badaccess - iphone

I have a view controller, when it's dissming with completion, I post a notfication, and in a subview which contained in another view controller, has added as a oberserve. But, when it tries to execute post notificaiton methode, exec_bad_access happend. what's wrong? The codes are:
BrandListByIdViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSNumber *bid = self.brands[indexPath.row][#"id"];
[self dismissViewControllerAnimated:YES completion:^{
[[NSNotificationCenter defaultCenter] postNotificationName:#"SelectedBrandId" object:nil];
}];
}
SearchNewProduct.h
#interface SearchNewProduct : UIView
#end
SearchNewProduct.m
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didSelectedBrandId::) name:#"SelectedBrandId" object:nil];
}
}
- (void)didSelectedBrandId:(NSNotification *)notif{
NSLog(#"%s", __PRETTY_FUNCTION__);
}
Even I get rid of the userInfo, still bad access. I created a similar situation in another new project, it works fine.

I didn't realize that you were dealing with a UIView and not a UIViewController (should have read your question better). I think what is happening is that the View is receiving notifications even after being released. Make sure to call dealloc in your UIView and remove itself as an observer:
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Also, put an NSLog in that UIView's initWithFrame method to see if it is being called more than once.
This question is very similar:
ios notifications to "dead" objects

Not sure if this is the reason, but when you add your view to the notification center, your selector is wrong:
selector:#selector(didSelectedBrandId::)
There should only be one colon. The entire line should be:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didSelectedBrandId:) name:#"SelectedBrandId" object:nil];
Two colons indicates the method takes two arguments, but it only takes one.

Related

Reload table not working when app comes to foreground

i want to reload table when app comes to foreground from background, in my app delegate.m i did like this, but its not working
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSLog(#"applicationWillEnterForeground");
[[NSNotificationCenter defaultCenter] postNotificationName:#"EnteredForeground"
object:nil];
}
and in my viewController i am working like
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(whenAppEnteredIntoForeground:) name:#"EnteredForeground" object:nil];
}
- (void)whenAppEnteredIntoForeground:(id)object {
NSLog(#"log msg");
[tblSearch reloadData];
}
what should i do? what mistake i am doing? any help please
Firstly, you don't need to rebroadcast a notification when the app comes to the foreground, you can register for the notification from your view controller.
In your case, it's likely to be that the view is not loaded until after your secondary notification is sent, which is why your view controller cannot respond to it. Using breakpoints will confirm if this is the case.
Use this instead:
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(whenAppEnteredIntoForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
}
And you don't need to rebroadcast a notification from your appdelegate.
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(appEnteredIntoForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
}
- (void)appEnteredIntoForeground:(id)object {
[tableView reloadData];
}
Make sure that your Reload Data must call from main Thread Otherwise it will not be reloaded.

Adding an observer to each view or present view dynamically

I am posting a notification locally in the app when ever I receive a remote notification.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(#"Received notification: %#", userInfo);
[[NSNotificationCenter defaultCenter] postNotificationName:#"NEWMESSAGE" object:nil userInfo:userInfo]; }
I have added an observer to the view in the function viewWillAppear() and remove the observer in viewWillDisappear().
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(newMessageReceived:) name:#"NEWMESSAGE" object:nil];
and
[[NSNotificationCenter defaultCenter] removeObserver:self];
My question is I want to override every viewWillAppear and viewWillDisappear functions in all *.m files that use these functions in my app.
or how can I dynamically add an observer (like above) to the present view and remove the observer when that view disappears. it should be like a global action whenever view changes observer to be added and removed when it changes again.
Is this possible? if so please guide me.
thanks in advance.
Some thoughts:
You can subclass UIViewController and implement these method in the subclass-ed view controller class. Then you need to create all your views as the subclass of this UIViewController.
Example:
//Creating a custom subclass of UIViewController
#interface CustomViewController : UIViewController
#end
#implementation CustomViewController
- (void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(newMessageReceived:) name:#"NEWMESSAGE" object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#end
And create all your view controller as the subclass of CustomViewController.

NSNotification not receiving in View Controller from appDelegate

hello i assign nsnotifiaction in app delegate.m's method and this method call eprox every 30sec, and i wants its notifcation in viewcontroller adn execute method,
here is my code of appdelegate .m
- (void)layoutAnimated:(BOOL)animated{
BOOL yy= self.bannerView.bannerLoaded;
if (yy==1){
self.iAdString=[NSMutableString stringWithString:#"1"];
[[NSNotificationCenter defaultCenter] postNotificationName:#"BannerViewActionWillBegin" object:self];
}
else{
self.iAdString=[NSMutableString stringWithString:#"0"];
[[NSNotificationCenter defaultCenter] postNotificationName:#"BannerViewActionDidFinish" object:self];
}
}
and in viewcontroller.m
//i defined in viewdidload method
- (void)viewDidLoad{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willBeginBannerViewActionNotification:) name:#"BannerViewActionWillBegin "object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didFinishBannerViewActionNotification:) name:#"BannerViewActionDidFinish" object:nil];
}
its method are..
- (void)willBeginBannerViewActionNotification:(NSNotification *)notification{
[self.view addSubview:self.app.bannerView];
NSLog(#"come");
}
- (void)didFinishBannerViewActionNotification:(NSNotification *)notification {
NSLog(#"come");
[self.app.bannerView removeFromSuperview];
}
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
I have not getting response of excessing method while method read in appdelegate file.
Please help me.
You have a typo error.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willBeginBannerViewActionNotification:) name:#"BannerViewActionWillBegin "object:nil];
//Your error here-------------------------------------------------------------------------------------------------------------------------------------^
You have put a space there.
SideNote: For all notification names, you should/can create a separate file and put all your notification names as constants strings.
const NSString *kBannerViewActionWillBegin=#"BannerViewActionWillBegin";
this will be easier to change the value and no such typo will happen.
From your code what I get is except for the notification name's all other stuff is fine, did you check whether the notification gets fired. Try keeping break points at the notification firing line.

Call [tableView reloadData]; on a viewController from a modalViewController

I have a modalViewController that comes up over the top of a viewController with a tableView. When the user clicks a button on the modalViewController I want to reload the tableView within the viewController with this:
[tableView1 reloadData];
I do not want to put the reload in the viewDidAppear or viewWillAppear methods as they get called when i do not need the tableView to reload (i.e. when the user clicks the back button to return to the tableView).
Is there a way to do this?
Try
1) write one method which reloads the table data.
2) Call it on the back button clicked.
This is the classic delegate pattern problem, in your modal view controller you need a delegate reference to the current view controller presenting it
//Modal
#protocol ModalVCDelegate
- (void)tappedBackButton;
#end
#class ModalVC: UIViewController
#property id<ModalVCDelegate> delegate;
#end
#implementation
- (void)backButtonTapped:(id)sender
{
if (self.delegate)
[self.delegate tappedBackButton];
}
#end
Now, in your presenting VC, just process this delegate message
//Parent VC
- (void)showModal
{
ModalVC *vc = [ModalVC new];
vc.delegate = self;
//push
}
- (void)tappedBackButton
{
[self.tableView reloadData];
//close modal
}
You can use delegate . If find it more harder then alternative is to use NSNotificationCenter. You can see accepted answer for Refreshing TableView. This is really very short, easy and understandable way.
using Notification like bellow Method:-
Create NSNotificationCenter at yourViewController's ViewdidLoad Mehod
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(ReloadDataFunction:)
name:#"refresh"
object:nil];
[super viewDidLoad];
}
-(void)ReloadDataFunction:(NSNotification *)notification {
[yourTableView reloadData];
}
Now you can Call this Notification from your modelViewController BackButton or else you want from calling this Refresh notification like putting this line of code:-
[[NSNotificationCenter defaultCenter] postNotificationName:#"refresh" object:self];
NOTE: postNotificationName:#"refresh" this is a key of particular Notification
Try to use this one
Make a Button and click on this button and than you can reload your data.
This button make custom and use it on background.
- (IBAction)reloadData:(id)sender
{
[tblView reloadData];
}
You can use NSNotification to refresh table on ViewController.
Inside viewController :
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
Write code in viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(reloadMainTable:)
name:#"ReloadTable"
object:nil];
- (void) reloadMainTable:(NSNotification *) notification
{
[tableView reload];
}
Inside ModelViewController:
[[NSNotificationCenter defaultCenter]
postNotificationName:#"ReloadTable"
object:nil];
Here you can also send custom object instead of nil parameter. But be care full about removal of NSNotification observer.

Pass An Object Between 2 View Controllers

I'm trying to pass an object between 2 VCs, from a popover to the detail view of split view controller.
I think I need to use NSNotificationCenter.
I tried this but can't seem to get it to work.
In didSelectRow of popover
[[NSNotificationCenter defaultCenter] postNotificationName:#"PassObject" withObject:objectToPass];
In detail VC
- (void) didReceiveNotificationPassObject:(NSNotification*)notification
{
YourObjectClass *theObject = (YourObjectClass*)notification.object;
}
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didReceiveNotificationPassObject:) name:#"PassObject" object:nil];
}
Probably just a typo when entering the question but in the first line where you post the notification
[[NSNotificationCenter defaultCenter] postNotificationName:#"PassObject" withObject:objectToPass];
the method signature is wrong - it should be 'object:objectToPass' not 'withObject:objectToPass'. The line you have there will compile with a warning and crash at runtime.
Aside from that all the logic seems fine.
What is the problem you are facing? Does didReceiveNotificationPassObject: hit? If it doesn't, you could verify that viewDidLoad executes before didSelectRow.
Use [[NSNotificationCenter defaultCenter] postNotificationName:#"PassObject" object:objectToPass]; instead of [[NSNotificationCenter defaultCenter] postNotificationName:#"PassObject" withObject:objectToPass];
Also, don't forget to removeObserver in viewDidUnload.
HTH,
Akshay
A fast and easy solution to notify with multiple parameters is to call the notification it like this
[[NSNotificationCenter defaultCenter] postNotificationName:#"shareButton" object:#"camera"];
Where "camera" acts like your parameter. Then
- (void)shareButton:(id)sender
{
NSString *kindOf = [sender object];
if ([kindOf isEqualToString:#"camera"]) {
// Your code goes here
}
}