Skip to main content

Posts

Key-Value-Observation — Looking for a more elegant solution to respond to value changes

I've run into a frustrating feature of KVO: all notifications are funneled through a single method ( observeValueForKeyPath:.... ), requiring a bunch of IF statements if the object is observing numerous properties. The ideal solution would be to pass a method as an argument to the method that establishes the observing in the first place, but it seems this isn't possible. Does a solution exist to this problem? I initially considered using the keyPath argument ( addObserver:forKeyPath:options:context: ) to call a method via NSSelectorFromString , but then I came across the post KVO Dispatcher pattern with Method as context and the article it linked to which offers a different solution in order to pass arguments along as well (although I haven't gotten that working yet). I know a lot of people have come up against this issue. Has a standard way of handling it emerged?

returning UIImage from block

I have the following code: - (UIImage *) getPublisherLogo { //check the cache if the logo already exists NSString * imageUrl = [NSString stringWithFormat:@"%@/%@&image_type=icon", self.baseUrl, self.imageUrl_]; ASIHTTPRequest * imageRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:imageUrl]]; [imageRequest setTimeOutSeconds:30.0]; [imageRequest setDownloadCache:[ASIDownloadCache sharedCache]]; [imageRequest setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy]; [imageRequest setCachePolicy:ASIAskServerIfModifiedWhenStaleCachePolicy|ASIFallbackToCacheIfLoadFailsCachePolicy]; [imageRequest setCompletionBlock:^(void){ UIImage *img = [UIImage imageWithData:[imageRequest responseData] ]; if (img){ return img; } }]; [imageRequest setFailedBlock:^(void){ NSLog(@"Error in pulling image for publisher %@", [[imageReq

What happens to my subView when changing navigation controllers?

Lets say that I add a subView to my view, like this: [[self addSubview:myView]; Then I push a new view onto the navigationController stack, like this: [self.navigationController pushViewController:otherView animated:YES]; What happens to the subView that I added to the original view. Is it removed automatically? Also, if my program calls this line: [[self addSubview:myView]; multiple times without removing the view does that do anything bad like create a memory leak?

Adding box2d header causes a torrent of compiler errors

I am experiencing either an extremely weird error or a temporary loss of brain function. I have a box2d / cocos project running in XCode. This all works fine but my code is currently a proof of concept "hack". Stage one of cleaning up the code is creating classes for various objects. Now, if I create a brand new Objective C class (inherited off NSObject) as below it all compiles fine. As soon as I attempt to import the "Box2D.h" I receive a gazillion errors originating from the box2d library saying it cannot be built. 201 to be precise and it indicates no issues with the class itself. Can anyone shed any light? I have already tried a clean and rebuild as well as restarting XCode. My super complicated class definition #import "Box2D.h" @interface test : NSObject @end An example compiler error thrown Expected '=', ',', ';', 'asm' or '__attribute__' before 'b2Fixture' in /Users/..../libs/Box2D/Dynamics

pushNavigationController on iOS 5 messed up my UINavigationBar when I pop back to the previous view controller

My main view controller only supports landscapeleft and landscaperight. This viewcontroller in the viewDidLoad has: if ([self.navigationItem respondsToSelector:@selector(leftItemsSupplementBackButton)]) { self.navigationItem.leftItemsSupplementBackButton = YES; self.navigationItem.leftBarButtonItem = homeBBI; NSArray *rightNavItems = [NSArray arrayWithObjects:searchBBI, allBBI, nil]; self.navigationItem.rightBarButtonItems = rightNavItems; } When I push my ReportViewController onto the stack, it looks fine. This controller also supports landscape only. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return UIInterfaceOrientationIsLandscape(interfaceOrientation); } When I rotate the iPad 180 degrees, all looks fine. However when I pop to the main view controller, the UINavigationBar gets messed up. It basically has all the items, including the title squished to the left. The search bar I had ends up taking up 75% of

Open MWPhotoBrowser with a link and loop

I'm fairly new at iOS development so please forgive my ignorance. I have web view with a search form, sending request to display different pages depending on the search query. When displaying a selected page I want to have a normal html link at the top which will give the user the possibility to display all images available using the very nice MWPhotoBrowser. So somehow I need to tell Xcode to open MWPhotoBrowser using a link and when this link is pressed send a variable to MWPhotoBrowser and in that viewController somehow loop through a my url.com/variable. Is this the best way to do this and is it even possible? I have searched as much as possible but I'm not sure what to look for so I would really appreciate if you could point me in the right direction.

Sending encoded XML to a server from iPhone

I'm communicating between an iPhone app and a server using XML. To get around the problem of the user entering any character that might break the XML before I send it to the server i'm using the NSString method: [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; But I noticed when doing a log of the string that the & symbol is not escaped? I thought that it should be? spaces, quotation marks and hash symbols which I've tested for all encode fine but not the ampersands? What is the most common methods used for encoding/decoding XML sent to/received from a server? Thanks.