2013-03-01 47 views
0

我想从我的用户在一个TextField中获取一个URL,然后处理结果。当我提供URLWithString: (NSString *)self.urlNameInput]时,下面的NSURLRequest中正在抛出以下-[UITextField length]: unrecognized selector sent to instancexcode textfield到NSURL路径

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; 

} 

回答

3

,我认为你的错误是,你传递一个的UITextField,以期待的NSString的方法。 更改

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];