Archive for August, 2012

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 […]