Some things on the iPhone are more complicated than they need to be. One example is the use of modal UIAlertViews to ask the user for feedback before continuing with the execution of the program. In a pseudo code notation, it looks like this:
[sourcecode language="objc"]
startWithSomething()
int res = Inputbox("Do you want to save the data?", Yes, No)
if (res==1) {
doSomething()
} else {
doSomethingElse()
}
continueWithSomething()
[/sourcecode]
This pseudo code snippet would wait until the user either decides Yes and then execute doSomething() followed by continueWithSomething() or doSomethingElse() followed by continueWithSomething().
If one decides to implement the same on the iPhone, we need to do a few additional things. First of all, we need to create an UIAlertView delegate that implements the UIAlertViewDelegate protocol. Then, we need to assure that the delegate informs us about the user decision. We do this by generating events to which we attach event observers:
[sourcecode language="objc"]
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doSomething:) name:@"UserSaysYes" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doSomethingElse:) name:@"UserSaysNo" object:nil];
[/sourcecode]
This code listens for events with the name UserSaysYes (UserSaysNo) and calls the method doSomething (doSomethingElse) if the corresponding event can be observed.
In the UIAlertViewDelegate we need to generate the events by adding the following code:
[sourcecode language="objc"]
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex=0) {
NSMutableArray *keys = [NSMutableArray arrayWithObjects:@"Info", nil];
NSMutableArray *objects = [NSMutableArray arrayWithObjects:@"Yes Sir!" , nil];
NSDictionary *info = [[NSDictionary dictionaryWithObjects:objects forKeys:keys]];
[[NSNotificationCenter defaultCenter]postNotificationName:@"UserSaysYes" object:info];
} else {
NSMutableArray *keys = [NSMutableArray arrayWithObjects:@"Info", nil];
NSMutableArray *objects = [NSMutableArray arrayWithObjects:@"No Sir!" , nil];
NSDictionary *info = [[NSDictionary dictionaryWithObjects:objects forKeys:keys]];
[[NSNotificationCenter defaultCenter]postNotificationName:@"UserSaysNo" object:info];
}
[/sourcecode]
The implementation of doSomething (doSomethingElse) is straightforward: we parse the notification object and print the content on the console. Then we call continueWithSomething in order to complete our example.
[sourcecode language="objc"]
-(void) doSomething:(NSNotification *) notification {
NSDictionary *info = [notification object];
NSstring* text = [info objectForKey:@"Info"];
NSLog(text);
[self continueWithSomething];
}
-(void) doSomethingElse:(NSNotification *) notification {
NSDictionary *info = [notification object];
NSstring* text = [info objectForKey:@"Info"];
NSLog(text);
[self continueWithSomething];
}
[/sourcecode]
The drawback of this method is obviously that the modal logic is scattered among different method implementations which can make a program hard to read. However, if you have one or two places in your code that need modal user input, this is certainly a way to do it.
Your ikangai team
Tags: Code Snippet of the Week, Modal, UIAlertView