2013-03-01 2 views
0

J'essaie d'obtenir une URL de mon utilisateur dans un TextField et ensuite traiter le résultat. Actuellement, une exception est lancée avec le -[UITextField length]: unrecognized selector sent to instance suivant dans NSURLRequest ci-dessous lorsque je fournis URLWithString: (NSString *)self.urlNameInput].xcode textfield to NSURL chemin

myViewController.h

@class myViewController; 
@interface myViewController : UIViewController <UITextFieldDelegate, NSURLConnectionDataDelegate, NSURLConnectionDelegate> 
{ 
    NSMutableData *receivedData; 
} 
@property (weak, nonatomic) IBOutlet UITextField *urlNameInput; 
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController; 
@end 

myViewController.m

- (BOOL)textFieldShouldReturn:(UITextField *)textField { 
    if (textField == self.urlNameInput) { 
     [textField resignFirstResponder]; 
     NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString: (NSString *)self.urlNameInput] cachePolicy:NSURLRequestUseProtocolCachePolicy 
               timeoutInterval:60.0]; 
     // create the connection with the request 
     // and start loading the data 

     NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
     if (theConnection) { 
      // Create the NSMutableData to hold the received data. 
      // receivedData is an instance variable declared elsewhere. 
      receivedData = [NSMutableData data] ; 
     } else { 
      // Inform the user that the connection failed. 
      NSLog(@"Their is an error with that URL."); 
     } 

    } 
    return YES; 

} 

Répondre

3

Je pense que votre erreur est que vous passez un UITextField à une méthode qui attend un NSString. Changer

NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString: (NSString *)self.urlNameInput] cachePolicy:NSURLRequestUseProtocolCachePolicy 
              timeoutInterval:60.0]; 

à

NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlNameInput.text] cachePolicy:NSURLRequestUseProtocolCachePolicy 
              timeoutInterval:60.0];