UIAlertViewStylePlainTextInput を使用

UIWebView で、JavaScript の prompt をハンドルして AlertView を表示するなら、

UIAlertViewStylePlainTextInput を使った方が、iPadiPhone での画面の大きさを考慮して
TextField の位置を調整などの面倒なことをしなくて済む。


-(NSString *)webView:(UIWebView *)sender runJavaScriptTextInputPanelWithPrompt:(NSString *)message 
         defaultText:(NSString *)defaultText initiatedByFrame:(WebFrame *)frame
{
    UIAlertView *dialog = [ [UIAlertView alloc] initWithTitle:@"Title"
                                                      message:message
                                                     delegate:self 
                                            cancelButtonTitle:@"cancel"
                                            otherButtonTitles:@"OK", nil
                          ];
    [dialog setAlertViewStyle: UIAlertViewStylePlainTextInput];

    UITextField *textField = [dialog textFieldAtIndex:0];
    [textField setText:defaultText];
    [dialog show];

    while(dialog.hidden == NO && dialog.superview != nil)
        [[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01f]];

    [dialog autorelease];

    if (dialog_result){
        return [textField text];
    }else {
        return defaultText;
    }
}