Search Results

Search found 11 results on 1 pages for 'nsnotifications'.

Page 1/1 | 1 

  • NSNotifications vs delegate for multiple instances of same protocol

    - by Brent Traut
    I could use some architectural advice. I've run into the following problem a few times now and I've never found a truly elegant way to solve it. The issue, described at the highest level possible:I have a parent class that would like to act as the delegate for multiple children (all using the same protocol), but when the children call methods on the parent, the parent no longer knows which child is making the call. I would like to use loose coupling (delegates/protocols or notifications) rather than direct calls. I don't need multiple handlers, so notifications seem like they might be overkill. To illustrate the problem, let me try a super-simplified example: I start with a parent view controller (and corresponding view). I create three child views and insert each of them into the parent view. I would like the parent view controller to be notified whenever the user touches one of the children. There are a few options to notify the parent: Define a protocol. The parent implements the protocol and sets itself as the delegate to each of the children. When the user touches a child view, its view controller calls its delegate (the parent). In this case, the parent is notified that a view is touched, but it doesn't know which one. Not good enough. Same as #1, but define the methods in the protocol to also pass some sort of identifier. When the child tells its delegate that it was touched, it also passes a pointer to itself. This way, the parent know exactly which view was touched. It just seems really strange for an object to pass a reference to itself. Use NSNotifications. The parent defines a separate method for each of the three children and then subscribes to the "viewWasTouched" notification for each of the three children as the notification sender. The children don't need to attach themselves to the user dictionary, but they do need to send the notification with a pointer to themselves as the scope. Same as #4, but rather than using separate methods, the parent could just use one with a switch case or other branching along with the notification's sender to determine which path to take. Create multiple man-in-the-middle classes that act as the delegates to the child views and then call methods on the parent either with a pointer to the child or with some other differentiating factor. This approach doesn't seem scalable. Are any of these approaches considered best practice? I can't say for sure, but it feels like I'm missing something more obvious/elegant.

    Read the article

  • Best way to update badgeValue of UITabBarController from a UIView

    - by Rudiger
    Hi everyone, I have a tabBarController set up in the AppDelegate and have a few UIViewControllers with Nav Controllers. In one of the TabBar items, after I have pushed a few UIViews I want to update the badgeValue item of a different TabBar item. Whats the best way to do this? The only way I can really think is a NSNotification and a singleton storage for the value, but it seems a lot of work for something simple, that and I have no idea about NSNotifications. I had a wild guess at something like super.tabBarController.otherView.badgeValue = @"1" (as I set which tab is selected in a similar way) but I'm not surprised this doesn't work. Thanks

    Read the article

  • NSNotification vs. Delegate Protocols?

    - by jr
    I have an iPhone application which basically is getting information from an API (in XML, but maybe JSON eventually). The result objects are typically displayed in view controllers (tables mainly). Here is the architecture right now. I have NSOperation classes which fetch the different objects from the remote server. Each of these NSOperation classes, will take a custom delegate method which will fire back the resulting objects as they are parsed, and then finally a method when no more results are available. So, the protocol for the delegates will be something like: (void) ObjectTypeResult:(ObjectType *)result; (void) ObjectTypeNoMoreResults; I think the solution works well, but I do end up with a bunch of delegate protocols around and then my view controllers have to implement all these delegate methods. I don't think its that bad, but I'm always on the lookout for a better design. So, I'm thinking about using NSNotifications to remove the use of the delegates. I could include the object in the userInfo part of the notification and just post objects as received, and then a final event when no more are available. Then I could just have one method in each view controller to receive all the data, even when using multiple objects in one controller.† So, can someone share with me some pros/cons of each approach. Should I consider refactoring my code to use Events rather then the delegates? Is one better then the other in certain situations? In my scenario I'm really not looking to receive notifications in multiple places, so maybe the protocol based delegates are the way to go. Thanks!

    Read the article

  • Network Connection NSNotification for OSX?

    - by Andrew
    I simply need to have a notification post when a valid IP address is assigned. I have tried polling via SCReachability, but that seems to be inefficient. Any suggestions? This seems like it should be simple, but I've been struggling for several hours to get anything working.

    Read the article

  • How do I make my NSNotification trigger a selector?

    - by marty
    Here's the code: - (void)viewDidLoad { [super viewDidLoad]; NSURL *musicURL = [NSURL URLWithString:@"http://live-three2.dmd2.ch/buureradio/buureradio.m3u"]; if([musicURL scheme]) { MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:musicURL]; if (mp) { // save the music player object self.musicPlayer = mp; [mp release]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(popBack:) name:@"MPMoviePlayerDidExitFullscreenNotification" object:nil]; // Play the music! [self.musicPlayer play]; } } } -(void)popBack:(NSNotification *)note { [self.navigationController popToRootViewControllerAnimated:YES]; } The selector method never gets called. I just want to pop back to the root menu when the "Done" button is pressed on the movie player. I put an NSLog in the selector to check if it was even being called, nothing. The music plays fine. Any thoughts?

    Read the article

  • NSNotification on multiple objects

    - by Jan Hendrix
    Hi, In my NSApp delegate I add an observer of an object that is an NSWindow subclass that gets initiated in the delegate itself and that posts a notification once the window gets clicked. The selector is also in the delegate. From that same delegate class I initiate another object which when initiated adds itself as an observer for another window of the same NSWindow subclass of above and the selector is in this newly initiated class too. Both notifications get posted but the problem is that they get posted in both classes... Is this normal? I was hoping that it only got posted once.

    Read the article

  • Common NSNotification mistakes?

    - by Ben Packard
    A simplification... A building has an array of apartment objects. Each apartment has a single currentTenant. These tenants are of type Person. Note that currentTenant doesn't have a reference to the apartment, so can't send information back up the chain. When a tenant has a plumbing issue he raises an NSNotification: [nc postNotificationName:@"PlumbingIssue" object:self]; Each Apartment observes notifications ONLY FROM it's own current tenant (this is set up when the apartment is built, before there is a current tenant): [nc addObserver:self selector:@selector(alertBuildingManager:) name:@"PlumbingIssue" object:[self currentTenant]; When the apartment receives a notification from it's own currentTenant, it sends it's own notification, "PlumberRequired", along with the apartment number and the currentTenant in an NSDictionary. Apartment observes these notifications, which it will take from any apartment (or other object): [nc addObserver:self selector:@selector(callPlumber) name:@"PlumberRequired" object:nil]; Is there something I could be getting fundamentally wrong here? What's happening is that the apartment is receiving notifications from any and all currentTenants, rather than jus it's own. Sorry that the actual code is a bit too unwieldy to post. Was just wondering if there's a gap in my understanding about observing notifications from a particular sender?

    Read the article

  • Where should I remove a notification observer?

    - by nevan
    I set up a notification observer in my view controll init method like so: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveState) name:UIApplicationWillResignActiveNotification object:nil]; Where is the best place to call removeObserver:name:object: for this notification. I'm currently calling it in my dealloc method, but wanted to know if that might cause problems.

    Read the article

  • How to remove an observer for NSNotification in a UIView?

    - by sudo rm -rf
    Hello! I've added an observer in a custom UIView I've created under initWithFrame:. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateZipFromLocation:) name:@"zipFoundFromLocation" object:nil]; The problem is, this view is a subview. When the view is loaded again, it calls the initWithFrame message again, thus adding two observers and so on. How can I remove the observer when the view is going to disappear? Since it is a UIView, it says that viewWillDisappear:(BOOL)animated is not a valid method. Any ideas?

    Read the article

  • Sync Services With AddressBook

    - by Marcus
    Hi, I've developed an application to sync the Mac address book with another application. I was using an NSNotification (kABDatabaseChangedExternallyNotification) which are sent every time the address book is edited to do the syncing. However I found that if I 'overloaded' the address book (by adding 20 records really quickly or something) I didn't get all of the notifications. I have since found the Sync Services framework which seems to be aimed at my kind of application and it seems to work fine. The problem is that the changes are not 'pushed' to me in the same was as an NSNotification - I have to sync every 5 minutes or so. And, from what I can understand, I shouldn't use the AddressBook API directly but push changes back through sync services. This means that changes are not instant but lagged by how often address book/my app syncs. Is there any way to make it instant? or to use the NSNotifications reliably? Thanks for your time, Marc

    Read the article

  • Pass Result of ASIHTTPRequest "requestFinished" Back to Originating Method

    - by Intelekshual
    I have a method (getAllTeams:) that initiates an HTTP request using the ASIHTTPRequest library. NSURL *httpURL = [[[NSURL alloc] initWithString:@"/api/teams" relativeToURL:webServiceURL] autorelease]; ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:httpURL] autorelease]; [request setDelegate:self]; [request startAsynchronous]; What I'd like to be able to do is call [WebService getAllTeams] and have it return the results in an NSArray. At the moment, getAllTeams doesn't return anything because the HTTP response is evaluated in the requestFinished: method. Ideally I'd want to be able to call [WebService getAllTeams], wait for the response, and dump it into an NSArray. I don't want to create properties because this is disposable class (meaning it doesn't store any values, just retrieves values), and multiple methods are going to be using the same requestFinished (all of them returning an array). I've read up a bit on delegates, and NSNotifications, but I'm not sure if either of them are the best approach. I found this snippet about implementing callbacks by passing a selector as a parameter, but it didn't pan out (since requestFinished fires independently). Any suggestions? I'd appreciate even just to be pointed in the right direction. NSArray *teams = [[WebService alloc] getAllTeams]; (currently doesn't work, because getAllTeams doesn't return anything, but requestFinished does. I want to get the result of requestFinished and pass it back to getAllTeams:)

    Read the article

1