2011-08-12 83 views
0

我有一個按鈕「getResponse」,它在將IP地址放入UITextField中之後向服務器發出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]; 
    } 
} 
+0

設置的UITextField的委託到的UIViewController需要更多的信息。發生什麼事?你期望發生什麼? – ColdLogic

+0

該按鈕正在執行調用XMLRPC服務器的代碼。我首先在UITextBox中放入一個IP地址,然後按下getResponse IBAction。我希望它首先辭去鍵盤,然後調用XMLRPC服務器並顯示數據。就像現在這樣,服務器首先被調用,然後數據通過,然後鍵盤退出。 – swbooking

回答

1

而不是

[self serverInputReturn:self]; 

嘗試

[self serverInputReturn:self.serverInput]; 

你需要傳遞的UITextField不是UIViewController中。

確保您通過

self.serverInput.delegate = self; 
+0

不,仍然工作相同:按getResponse,調用服務器,獲取信息,然後釋放鍵盤... – swbooking

+0

你可以直接嘗試,[self.serverInput resignFirstResponder]? – mbh

+0

另外檢查serverInput是否確實是第一個響應者,通過serverInput.isFirstResponder – mbh

相關問題