按下按钮后立即退出键盘

时间:2011-08-12 18:13:57

标签: iphone ios xml-rpc

我有一个按钮“getResponse”,它在您在UITextField中输入IP地址后对服务器进行XMLRPC调用。我喜欢“getResponse”按钮,如果键盘向上调用服务器,则首先重新调整键盘。

就像现在一样,如果键盘已启动,它将调用服务器,如果找不到有效的服务器则会检索信息或错误,然后重新调整键盘。

我知道它可能很容易,但我不能为我的生活弄清楚。任何帮助都会很棒,谢谢!

代码:

小时。

    #import <UIKit/UIKit.h>
#import "XMLRPCResponse.h"
#import "XMLRPCRequest.h"
#import "XMLRPCConnection.h"


@interface SecondViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate> {
    IBOutlet UILabel *helloResponse;
    IBOutlet UILabel *SumCountsResponse;
    IBOutlet UITextField *serverInput;
    IBOutlet UIPickerView *pickerView;
    IBOutlet UILabel *pickerTrap;
    IBOutlet UIAlertView *alert;

    NSMutableArray * pickerViewArray;

    int trapSelected;

}

@property(nonatomic,retain) IBOutlet UILabel *helloResponse; 
@property(nonatomic,retain) IBOutlet UILabel *SumCountsResponse;
@property(nonatomic,retain) IBOutlet UITextField *serverInput;
@property(nonatomic,retain) IBOutlet UIPickerView *pickerView;
@property(nonatomic,retain) IBOutlet UILabel *pickerTrap;
@property(nonatomic,retain) IBOutlet UIAlertView *alert;



- (IBAction)getResponse:(id)sender;
- (IBAction)serverInputReturn:(id)sender;
- (IBAction)backgroundTouched:(id)sender;


@end

米。

#import "SecondViewController.h"

@implementation SecondViewController

@synthesize helloResponse;
@synthesize SumCountsResponse;
@synthesize serverInput;
@synthesize pickerView;
@synthesize pickerTrap;
@synthesize alert;


-(IBAction)serverInputReturn:(id)sender { //resign keyboard on 'return' button

    [sender resignFirstResponder];

}

-(IBAction)backgroundTouched:(id)sender { //resign keyboard on background touch

    [serverInput resignFirstResponder];
}


-(IBAction)getResponse: (id) sender {

    [self serverInputReturn:self]; //trying to call method to resign keyboard first 

    //setup IP call for XMLserver
    NSString *server = serverInput.text;
    NSString *http = @"http://";
    server = [server stringByAppendingString:@":8080/RPC2"];
    server = [http stringByAppendingString:server];

    //Begin calls to XMLRPC server for data return
    XMLRPCRequest *requestHello = [[XMLRPCRequest alloc] initWithHost:[NSURL URLWithString:server]];
    [requestHello setMethod:@"hello" withObjects:[NSArray arrayWithObjects: nil]];
    NSString *resultHello = [self executeXMLRPCRequest:requestHello];
    [requestHello release];


    XMLRPCRequest *requestSumCounts = [[XMLRPCRequest alloc] initWithHost:[NSURL URLWithString:server]];
    [requestSumCounts setMethod:@"SumCountsString" withObjects:[NSArray arrayWithObjects: nil]];
    NSString *resultSumCounts = [self executeXMLRPCRequest:requestSumCounts];
    [requestSumCounts release];


    XMLRPCRequest *requestTrapCountX = [[XMLRPCRequest alloc] initWithHost:[NSURL URLWithString:server]];
    [requestTrapCountX setMethod:@"TrapCountString" withObjects:[NSArray arrayWithObjects: [NSNumber numberWithInt:trapSelected], nil]];
    NSString *resultTrapCountX = [self executeXMLRPCRequest:requestTrapCountX];
    [requestTrapCountX release];


    if (![resultHello isKindOfClass:[NSString class]]){
            alert = [[UIAlertView alloc] initWithTitle:@"Error: Invalid Server Address" message:@"Please Check Server Address and Try Again" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
            [alert show];
            [alert release];
    }
    else{
        helloResponse.text = resultHello;
        SumCountsResponse.text = resultSumCounts;
        pickerTrap.text = resultTrapCountX;

    }
    }
}


- (id)executeXMLRPCRequest:(XMLRPCRequest *)req {
    XMLRPCResponse *userInfoResponse = [XMLRPCConnection sendSynchronousXMLRPCRequest:req];
    if ([userInfoResponse isKindOfClass:[NSError class]]) {
        return alert;
    }
    else{
    return [userInfoResponse object];
    }
}

1 个答案:

答案 0 :(得分:1)

而不是

[self serverInputReturn:self];

尝试

[self serverInputReturn:self.serverInput];

您需要传递UITextField而不是UIViewController。

确保您通过

将UITextField的委托设置为UIViewController
self.serverInput.delegate = self;