Archive for the 'iOS' Category

iOS: UIAlertView as an Input Dialog

Problem: You want a simple input dialog to have the user enter a text in iOS. Solution: You can use an UIAlertView: UIAlertView * inputAlert = [[UIAlertView alloc] initWithTitle:@”New Event” message:@”Enter a title for the event” delegate:self cancelButtonTitle:@”Cancel” otherButtonTitles:@”OK”, nil]; inputAlert.alertViewStyle = UIAlertViewStylePlainTextInput; [inputAlert show]; You also need to add the UIAlertViewDelegate protocol to the […]

iOS: Running Code on the Main Thread

Problem: In iOS, updating the user interface should be done in the main thread. However, many asynchronous methods invoke the callback on an arbitrary thread. How can I make sure a particular piece of code is run in the main thread? Solution: You need to use the grand central dispatch: dispatch_async(dispatch_get_main_queue(),^{ // The code to […]

Setting Up A Navigation-based Application Programmatically

Problem: To avoid using singleton models in your iPhone/iPad apps, it’s necessary to create the root view programmatically. This way you’re able to inject the model at creation time instead of in the viewDidLoad method. Solution: Here’s how to set up a navigation-based application in code in XCode 4.2 (with automatic memory management enabled). In […]